无法从AppDelegate中实例化rootViewController

时间:2015-09-29 13:31:45

标签: ios xcode swift parse-platform

我的入职流程(登录/注册)生活在UINavigationController链之外,但是当登录被认证时,我在appdelegate中调用一个应该实例化rootViewController然后推送到它的方法。我尝试了两件事:

上面的代码段在appDelegate中从响应本地通知时调用的方法开始工作,但在这种情况下不是。

  self.window?.makeKeyAndVisible()
  self.window?.rootViewController?.navigationController?.popToRootViewControllerAnimated(true)

这是我尝试的另一种方法:

 let rootViewController = self.window?.rootViewController

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let dashboard = mainStoryboard.instantiateViewControllerWithIdentifier("DashboardViewController") as! DashboardViewController

   rootViewController!.navigationController?.popToViewController(dashboard, animated: true)

这些都不奏效。我做错了什么?

2 个答案:

答案 0 :(得分:2)

我的登录视图控制器也位于我的标签栏控制器之外,所以这就是我所做的:

enter image description here

在appDelegate中:

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.


    let storyboard  = UIStoryboard(name: "Main", bundle: nil)
    let loginVC = storyboard.instantiateViewControllerWithIdentifier("LoginVC") as! LoginViewController
    self.window?.rootViewController = loginVC
    self.window!.makeKeyAndVisible()


    return true
}

然后我在loginViewContoller.swift中有一个方法:

    @IBAction func loginPushed(sender: AnyObject) {


    //other stuff here 

    let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
    let tabBarController = UITabBarController()
    let first = mainStoryBoard.instantiateViewControllerWithIdentifier("firstNavController") as! UINavigationController
    let second = mainStoryBoard.instantiateViewControllerWithIdentifier("secondNavController") as! UINavigationController
    let third = mainStoryBoard.instantiateViewControllerWithIdentifier("thirdNavController") as! UINavigationController

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let controllers = [first, second, third]

    tabBarController.viewControllers = controllers
    appDelegate.window!.rootViewController = tabBarController
    appDelegate.window!.makeKeyAndVisible()

}

答案 1 :(得分:0)

我认为你不应该调用'popToRootViewController',你可以重新分配window.rootViewController~