简体   繁体   中英

Moving from a login Storyboard to the main Storyboard in Swift without Warning

I am trying to change view controller after a user logs into my app from the login storyboard to my main storyboard, this works successfully but I receive the following warning in the console:

2016-02-05 01:57:59.553 Commu[4749:160489] Warning: Attempt to present <UITabBarController: 0x7f93285b9d90> on <Commu.LoginViewController:     0x7f9328494e80> whose view is not in the window hierarchy!

The code in my login view controller is as follows:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   let vc = storyboard.instantiateViewControllerWithIdentifier("MainMenu")
   self.presentViewController(vc, animated: true, completion: nil)                         
})

Is there a better way to move between storyboards to avoid this warning message?

Once user login, login hierarchy no more required.

After successful login your need to change root view controller of your mainWindow, ie rootViewContoller of window of appDelegate.

Similarly change the rootViewContoller of window again when user logout.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MainMenu")


let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = vc

You can also include UINavigationController in hierarchy, to push and pop other viewControllers.

The warning is because:

self.presentViewController(vc, animated: true, completion: nil) is presenting the viewController as a modal view. Which should only be used to display a screen for a short period of time to give the user some actions. It is intended that the user will, at some point, move back from the modal window.

There is a new feature, called a storyboard reference . This allows you to use a segue between storyboards.

Then you could use:

self.performSegueWithIdentifier("segueId", sender: self)

This is a much cleaner way to do it and keeps all your navigation controls inside the storyboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM