2016-06-25 3 views
2

나는 사용 'checkOrientation'스위프트

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) 
     } 
    } 
ViewController.Swift

를 들어

AppDalgate.Swift

func application (application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { 
     return checkOrientation(self.window?.rootViewController) 

    } 

에게 AppDalgate.Swift이 코드를이 오류가

+0

재귀 적으로'checkOrientation'을 호출하여 얻으려는 것이 확실하지 않습니다. – Ike10

답변

3

앱 위임자는 View Controller의 기능에 액세스 할 수 없습니다. 당신이 달성하고자하는 경우, 하나 개의 옵션과 같이이 동일한 인스턴스 ViewControllerAppDelegate에 변수를 추가하고 설정하는 것입니다 :

//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은 가능한 간결하게 보관해야합니다. ,

func checkOrientation(viewController:UIViewController?)-> UIInterfaceOrientationMask { 
    if viewController is LoginViewController { 
     return UIInterfaceOrientationMask.Portrait 
    } else { 
     return UIInterfaceOrientationMask.All 
    } 
} 

마지막으로 완전히 checkOrientation을 제거하고 supportedInterfaceOrientationsForWindow에있는 그것의 논리를 넣어 고려 :

또한, 당신은 아마 당신의 checkOrientation 기능을 변경해야합니다. 예 :

+0

감사합니다! 어떻게 내보기에서 작동하도록 액세스하려면 – kvra13

+0

@ kvra13 내 편집을 참조하십시오. – Ike10

+0

자, 내가 편집 코드를 입력했을 때이 오류가 발생한다 'Int'타입의 반환 식을 'UIInterfaceOrientationMask'@ lke10으로 변환 할 수 없다. – kvra13