2017-08-31 112 views
0

我已經檢查了這個問題的大部分答案,但主要是Cocoa Touch。我需要在mac應用程序的appdelegate中實現此功能。在我的mac應用程序中添加自定義視圖控制器作爲根視圖控制器?

一旦用戶登錄,他將被重定向到主屏幕,否則進入登錄屏幕。

let controller:NSWindowController = NSWindowController() 
let viewController:NSViewController 

let storyboard = NSStoryboard.init(name: "Main", bundle: nil) 
    let stringLoginStatus = NSUserDefaults.standardUserDefaults().objectForKey(Constants.Key_LoginStatus) as? String 
    if stringLoginStatus != nil 
    { 
     if stringLoginStatus == "true" 
     { 
      viewController = storyboard.instantiateControllerWithIdentifier("Channel") as! NSViewController 
     } 
     else{ 
      viewController = storyboard.instantiateControllerWithIdentifier("ViewController") as! NSViewController 
     } 
    } 
    else{ 
     viewController = storyboard.instantiateControllerWithIdentifier("ViewController") as! NSViewController 
    } 

    controller.window?.contentViewController = viewController 
    controller.window?.makeKeyAndOrderFront(self) 

它顯示和錯誤,因爲控制器沒有被初始化。

+0

我甚至不知道'controller'和'viewController'是什麼。 –

+0

我編輯代碼請檢查@ElTomato –

回答

1

試試這個代碼更改您的視圖控制器: -

UIApplication.appWindow.rootViewController = UIStoryboard.main.instantiateViewController(withIdentifier: "customViewControl") 

// MARK:請UIApplication的擴展來獲得APPWINDOW

extension UIApplication { 
     class var appWindow: UIWindow! { 
      return (UIApplication.shared.delegate?.window!)! 
     } 
    } 

// MARK:讓故事板延伸得到主屏幕

extension UIStoryboard { 
    class var main: UIStoryboard { 
     let storyboardName: String = Bundle.main.object(forInfoDictionaryKey: "UIMainStoryboardFile") as! String 
     return UIStoryboard(name: storyboardName, bundle: nil) 
    } 

}

+0

UIApplication適用於Cocoa Touch或iPhone,iPad應用程序。我需要Cocoa,mac應用程序(NSApplication不是UIApplication)。 –

相關問題