【问题标题】:Use of unresolved identifier 'checkOrientation' Swift使用未解析的标识符“checkOrientation”Swift
【发布时间】:2016-06-25 00:29:16
【问题描述】:

当我尝试键入 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)
        }
    }

【问题讨论】:

  • 也不清楚你通过递归调用checkOrientation来获得什么

标签: xcode swift swift2 ios9 xcode7


【解决方案1】:

您的 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
   }
}

【讨论】:

  • 谢谢!如何在我的视图中访问功能
  • 好吧,当我输入编辑代码时出现此错误无法将“Int”类型的返回表达式转换为“UIInterfaceOrientationMask”类型@lke10
  • checkOrientation 更改为返回类型UIInterfaceOrientationMask 并删除.rawValue 并强制转换为Int 类型。 @kvra13(如我的新编辑所示)。
  • 我也会考虑将checkOrientation的逻辑直接移到supportedInterfaceOrientationsForWindow@kvra13并去掉checkOrientation
  • 你的意思是这样 return vc.check Orientation (Int (UIInterfaceOrientation Mask.All.rawValue)) @lke10
猜你喜欢
  • 2016-04-29
  • 1970-01-01
  • 2015-05-13
  • 2016-11-28
  • 2015-05-25
  • 2017-01-28
  • 2015-11-27
  • 1970-01-01
相关资源
最近更新 更多