How to return to the first storyboard view controller?

Here is the scenario:

The first scene in my storyboard is login. This is a UIViewController. When the user is logged in, he shows a home view built into the navigation controller. I am adding a logout function, which should return me to the first scene in the storyboard, which is the kind of login. How to do it?

Here is a storyboard image showing the input view β†’ navigation controller β†’ home view Storyboard

This is my implementation. In the logout action, I clear the session and go to the root controller. This does not work, because I still stick to the home view, as this is the root view controller of the navigation controller. However, if I restart the application, the user will be logged out and I will remain with the login view.

the code:

[self.navigationController popToRootViewControllerAnimated:NO]; // Set at beginning of storyboard UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; app.loginViewController = [mystoryboard instantiateViewControllerWithIdentifier:@"loginViewController"]; 
+8
ios objective-c storyboard
source share
4 answers

Use unwinding for this.

In your LoginViewController, declare a method with this signature

- (IBAction)unwindToLoginViewController:(UIStoryboardSegue*)segue

Go to your HomeViewController and drag the control from your exit button to the "Exit" button at the top of the view controller window (see screenshot below), then select unwindToLoginViewController segue. What is it!

enter image description here

+6
source share

U can appear with navigationController.viewControllers. Look up all view controllers among navigationController, define it and then click. If you clicked segue from LoginView into HomeView

 if([self.navigationController.viewControllers[0] isKindOfClass:[LoginViewController class]]) { [self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:YES]; } 

Hope this helps you ...

+2
source share

Try this answer. First you create a navigation controller. make it the "initial View Controller". After that, connect the Viewcontroller login as the root view controller and connect the home controller with the facebook Action button.

Navigation controller β†’ Input controller β†’ Home controller

Your storyboard looks like this:

enter image description here

After that, when you exit the HomeViewController, just add this method:

  -(IBAction)logOut_Action:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } 

Works Great. Please implement this and let me know if you encounter any problems. :)

+1
source share

Try the following:

 [self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil]; 
+1
source share

All Articles