首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Swift 4-如何在pods类中导入AppDelegate

Swift 4-如何在pods类中导入AppDelegate
EN

Stack Overflow用户
提问于 2018-08-14 06:20:01
回答 1查看 984关注 0票数 2

我正在尝试将此代码添加到cocoapods库内的视图控制器中

代码语言:javascript
复制
 public override func viewWillDisappear(_ animated: Bool) {
     (UIApplication.shared.delegate as! AppDelegate).restrictRotation = .portrait
}

但它使用了未声明的类型'AppDelegate‘错误..如何将AppDelegate等项目文件导入pods?

EN

回答 1

Stack Overflow用户

发布于 2018-08-14 06:38:55

Pod应该在不同的项目中可重用,因此您不会在Pod中包含项目文件。

你应该能够通过让你的pod定义一个协议,然后被你的项目App Delegate采用来实现你想要的:

代码语言:javascript
复制
protocol RotationRestrictable {
    enum RotationRestriction {
        case .none
        case .portrait
        case .landscape
    }

    var restrictRotation: RotationRestriction
}

然后你可以说

代码语言:javascript
复制
public override func viewWillDisappear(_ animated: Bool) {
    if let delegate = UIApplication.shared.delegate as? RotationRestrictable {
        delegate.restrictRotation = .portrait
    }
}

在你的App Delegate中:

代码语言:javascript
复制
import YourPod

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, RotationRestrictable {

    var restrictRotation: RotationRestrictable.RotationRestriction = .none {
        didSet {
           // Whatever you need to do
        }
    }
 ...
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51831335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档