我正在尝试将此代码添加到cocoapods库内的视图控制器中
public override func viewWillDisappear(_ animated: Bool) {
(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .portrait
}
但它使用了未声明的类型'AppDelegate‘错误..如何将AppDelegate等项目文件导入pods?
发布于 2018-08-14 06:38:55
Pod应该在不同的项目中可重用,因此您不会在Pod中包含项目文件。
你应该能够通过让你的pod定义一个协议,然后被你的项目App Delegate采用来实现你想要的:
protocol RotationRestrictable {
enum RotationRestriction {
case .none
case .portrait
case .landscape
}
var restrictRotation: RotationRestriction
}
然后你可以说
public override func viewWillDisappear(_ animated: Bool) {
if let delegate = UIApplication.shared.delegate as? RotationRestrictable {
delegate.restrictRotation = .portrait
}
}
在你的App Delegate中:
import YourPod
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, RotationRestrictable {
var restrictRotation: RotationRestrictable.RotationRestriction = .none {
didSet {
// Whatever you need to do
}
}
...
}
https://stackoverflow.com/questions/51831335
复制相似问题