Ios Swift 4-如何在pods类中导入AppDelegate
我正在尝试将此代码添加到cocoapods库中的视图控制器Ios Swift 4-如何在pods类中导入AppDelegate,ios,swift,cocoapods,Ios,Swift,Cocoapods,我正在尝试将此代码添加到cocoapods库中的视图控制器 public override func viewWillDisappear(_ animated: Bool) { (UIApplication.shared.delegate as! AppDelegate).restrictRotation = .portrait } 但它使用了未声明的类型“AppDelegate”错误。。如何将AppDelegate之类的项目文件导入到Pod中?Pod应可在不同的项目中重用,因此您
public override func viewWillDisappear(_ animated: Bool) {
(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .portrait
}
但它使用了未声明的类型“AppDelegate”错误。。如何将AppDelegate之类的项目文件导入到Pod中?Pod应可在不同的项目中重用,因此您不会在Pod中包含项目文件 通过让pod定义一个协议,然后由项目应用程序代表采用,您应该能够实现您的目标:
protocol RotationRestrictable {
enum RotationRestriction {
case .none
case .portrait
case .landscape
}
var restrictRotation: RotationRestriction
}
import YourPod
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, RotationRestrictable {
var restrictRotation: RotationRestrictable.RotationRestriction = .none {
didSet {
// Whatever you need to do
}
}
...
}
那么你可以说
public override func viewWillDisappear(_ animated: Bool) {
if let delegate = UIApplication.shared.delegate as? RotationRestrictable {
delegate.restrictRotation = .portrait
}
}
在您的应用程序代理中:
protocol RotationRestrictable {
enum RotationRestriction {
case .none
case .portrait
case .landscape
}
var restrictRotation: RotationRestriction
}
import YourPod
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, RotationRestrictable {
var restrictRotation: RotationRestrictable.RotationRestriction = .none {
didSet {
// Whatever you need to do
}
}
...
}