当我尝试 Type Return My Func 时,我在 AppDalgate.Swift 中收到此错误
AppDalgate.Swift
func application (application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
return checkOrientation(self.window?.rootViewController)
}
ViewController.Swift 的代码
func checkOrientation(viewController:UIViewController?)-> Int{
if(viewController == nil){
return Int(UIInterfaceOrientationMask.All.rawValue)//All means all orientation
}else if (viewController is LoginViewController){
return Int(UIInterfaceOrientationMask.Portrait.rawValue)//This is sign in view controller that i only want to set this to portrait mode only
}else{
return checkOrientation(viewController!.presentedViewController)
}
}
最佳答案
您的 App Delegate 无权访问您的 View Controller 中的功能。如果你想实现这一点,一个选择是向 AppDelegate
添加一个变量并将其设置为等于实例化的 ViewController
,如下所示:
//Add this to your AppDelegate Outside of the methods.
var vc = ViewController()
完成此操作后,您可以像这样从 AppDelegate 访问 ViewController 的方法:
//Inside a method of AppDelegate
//Replace ... with your parameters
vc.checkOrientation(...)
但是,请记住,这与您的应用程序完成启动后将使用的 ViewController
类实例不同。因此,如果您尝试使用一种引用应用启动后添加的数据的方法,它将不起作用。
此外,请记住,出于性能原因,AppDelegate
应尽可能简洁。
此外,您应该将 checkOrientation
函数更改为:
func checkOrientation(viewController:UIViewController?)-> UIInterfaceOrientationMask {
if viewController is LoginViewController {
return UIInterfaceOrientationMask.Portrait
} else {
return UIInterfaceOrientationMask.All
}
}
最后,考虑完全删除 checkOrientation
并将其逻辑放在 supportedInterfaceOrientationsForWindow
中。示例:
func application (application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if self.window?.rootViewController is LoginViewController {
return UIInterfaceOrientationMask.Portrait
} else {
return UIInterfaceOrientationMask.All
}
}
关于xcode - 使用未解析的标识符 'checkOrientation' Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38023821/