xcode - 使用未解析的标识符 'checkOrientation' Swift

标签 xcode swift swift2 ios9 xcode7

当我尝试 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/

相关文章:

swift - UI 未加载,代码仍在运行

swift 包装类

ios - Swift3:使用代码添加按钮

html - 以编程方式在网站上输入字符串

ios - ios8中的UIWebView透明背景

ios - Swift 4 中 strong 的搜索值

ios - 如何在没有动画的情况下以编程方式执行 "show (e.g Push)"segue?

ios - 更改动态字符串中的子字符串颜色

iphone - 有什么方法可以判断我的 iPhone 应用程序在运行时是否在调试器下运行?

xcode - 让 Matlab 的 mex 在 Mountain Lion 上与 xcode 4.4 一起工作