How can I go back to the initial view controller in Swift?

帅比萌擦擦* 提交于 2019-11-27 00:07:07

问题


So I have a login view, after successful login it goes to the first view of a navigation controller, then the user can go deeper to a settings view and then to a logout view. This logout should take the user back to the login view (which is not part of the navigation controller). It works with this code:

let loginViewController = self.storyboard!.instantiateViewControllerWithIdentifier("Login") as? LoginViewController
self.navigationController!.pushViewController(loginViewController!, animated: true)

But the login view displays the navigation bar at the top, which it shouldn't do, maybe there is something other than self.navigationController!.pushViewController that I should be using?


回答1:


SWIFT: You should use an unwind segue.

  1. First of all, put the following line in your FirstViewController:

    @IBAction func prepareForUnwind(segue: UIStoryboardSegue) {
    
    }
    

    The function actually doesn't have any code inside it.


  1. Now, go to your storyboard and create an unwind segue for LogoutViewController by control-dragging from the yellow button to the Exit button. Like this:

  1. Select the unwind segue created for FirstViewController.

  2. Change the segue identifier:


  1. Go to the code of LogoutViewController and just call the unwind segue normally:

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

    Swift 4

    self.performSegue(withIdentifier: "unwindToViewController1", sender: self)
    



回答2:


Look into unwind segueing if you are working with storyboards.

You just need to create unwind option in controller, that you want navigate to:

@IBAction func unwindToMe(segue: UIStoryboardSegue){}

Then create segue from storyboard.

And when you need to navigate back, just call the performSegue method with the unwind segue identifier that you just created.

If you want to do it only from code, than you just can write something like:

let loginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("Login")
UIApplication.sharedApplication().keyWindow?.rootViewController = loginViewController

In this case, you will set your app to initial state.




回答3:


If you have a Navigation controller, from your your controller use:

self.navigationController?.popToRootViewControllerAnimated(true)



回答4:


try it

    self.view.window?.rootViewController?.dismissViewControllerAnimated(true, completion: nil)

This will get you back to the beginning of the application flow.

Updated to Swift 4 (thanks @javaBeast)

    self.view.window?.rootViewController?.dismiss(animated: true, completion: nil) 



回答5:


I recommend you to make a segue from one ViewController to another, instead of pushing your ViewController like that.

So first, you need to Ctrl + clic from your first ViewController to your login ViewController, and then in the attribute inspector your give it an Identifier.

Then, all you have to do is this :

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

Now, for the navigation bar, I suggest you to remove the navigation controller from the login view, and associate it to your first view. It would remove the navigation bar from your login view.




回答6:


self.navigationController?.popToRootViewController(animated: true)

is the best option to go to first controller of navigation controller and then dismiss the navigation controller



来源:https://stackoverflow.com/questions/30052587/how-can-i-go-back-to-the-initial-view-controller-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!