Xcode 使用未解析标识符';检查方向';敏捷的

Xcode 使用未解析标识符';检查方向';敏捷的,xcode,swift,swift2,ios9,xcode7,Xcode,Swift,Swift2,Ios9,Xcode7,当我尝试键入Return My Func时,我在AppDalgate.Swift中遇到了这个错误 阿普达尔盖特,斯威夫特 func application (application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { return checkOrientation(self.window?.ro

当我尝试键入Return My Func时,我在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)
        }
    }

您的应用程序代理无权访问视图控制器中的功能。如果要实现这一点,一个选项是向
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
    }
}
<>最后,考虑删除<代码>检查定位>代码>,并将其逻辑放在<>代码>支持界面>窗口< /代码>。例如:

func application (application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
   if self.window?.rootViewController is LoginViewController {
      return UIInterfaceOrientationMask.Portrait
   } else {
      return UIInterfaceOrientationMask.All
   }
}

还不清楚通过递归调用
checkOrientation
谢谢,您想要获得什么!如何在我的视图中访问函数OK只是当我键入编辑代码时出现此错误无法将类型为“Int”的返回表达式转换为返回类型“UIInterfaceOrientationMask”@LKE10将检查方向更改为返回类型UIInterfaceOrientationMask,并删除.rawValue并强制转换为类型
Int
@KVRA13(如在我的新编辑中所示)。我还将把“代码”>检查方向> /代码>直接移动到<代码>支持的界面,窗口< <代码> @ kvRA13,并去掉<代码>检查方向>代码>您的意思是这样的返回VC.ChestOrror(Int(Ui接口FasePask.Auto.RWAWORD))@ LKE10。