使用未解析的标识符' checkOrientation'迅速

时间:2016-06-25 00:29:16

标签: xcode swift swift2 ios9 xcode7

当我尝试键入返回我的功能时,我在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)
        }
    }

1 个答案:

答案 0 :(得分:3)

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