Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 291
matt Avatar asked May 05 '15 12:05

matt


People also ask

How do I change my initial view controller?

You will be able to see how your Initial View Controller currently looks like on the left side of your screen. Simply drag the View Controller to the screen to create a new one; this option is located on the bottom-right side of the screen.

How do I change the initial view controller in storyboard?

Specifying the Initial View Controllerstoryboard and select the Tab Bar Controller Scene. On the right, select the Attribute inspector. You'll find a checkbox named Is Initial View Controller. Checking this box will identify the selected view controller as the initial entry point for the storyboard you're on.


2 Answers

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:

control-dragging in action

  1. Select the unwind segue created for FirstViewController.

  2. Change the segue identifier:

selected segue


changing the identifier of the segue

  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) 
like image 152
Allan Scofield Avatar answered Sep 23 '22 16:09

Allan Scofield


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

self.navigationController?.popToRootViewControllerAnimated(true) 
like image 43
Rene Ramirez Avatar answered Sep 25 '22 16:09

Rene Ramirez