简体   繁体   中英

How to unwind to the first view controller on a navigation stack

I have a problem with unwinding my view to login screen. The storyboard structure is as follow: Storyboard Structure

The user flow for the app is as follow: user login on LoginVC-> goes to main tab bar screen by modal segue-> on each tab bar item, I added right bar button on the navigation controller to access profile page, each tab bar item has independent navigation controller to keep the nav controller structure linear. -> once i click profile page button, profile page is presented modally -> when logout button on profile page is clicked, it triggers unwind segue and dismiss view controller

func logoutUser(){

        //Networking.logoutUser()
        print("It goes to login")
        self.performSegue(withIdentifier: "unwindToLogin", sender: self)
}

The unwind segue was implemented on LoginVC on the leftmost VC.I connected unwind segue on the profile screen and call it "unwindToLogin" I simply used performSegueWithIdentifier. However, the method does not get called and nothing happen to the view.

Edit 1: Im wondering since i call profile page modally on tab bar vc, it couldnt find the unwindtologin. If i simply use the instantiateviewcontoller to call login, will it clear my view controller stack ?

Edit 2: Sorry, i forgot that when i check if a user is logged in, i use the code below:

    if (FIRAuth.auth()?.currentUser != nil) {
        self.storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "TabBarViewController")
    }

So actually they can't find the unwind method because the root view is not main login view controller.

Can anyone help ?

Move to any View controller using unwind segue.

For moves to any view controller when you clicking a button.

- (IBAction)unwindToCurrentController:(UIStoryboardSegue *)unwindSegue { }

  • Add these above lines to your loginViewController.m file (or) add these lines to which view controller you want to move.
  • Actually you want to move to login view controller when clicking a button. So,create a button or choose any button for this event.
  • Before doing this you want to add the above code to your login view controller. Now add action to your button by dragging into Exit option on view controller on the Top .
  • It shows an unwind option unwindToCurrentController.Click that option and connect it.

Now Build and run your app. It works perfectly.

You need to create the unwind segue on every ViewController that you want to call it on. They can all use the same @IBAction destination in the login screen and they can all be called "unwindToLogin" .

I see the unwind segue defined on the MainTabBarController . In your UserProfileController , you need to control -drag from the view Controller icon to the Exit icon and then select your @IBAction in the pop up. Then find that unwind segue in the UserProfileController in the Document Outline and make its identifier "unwindToLogin" .


In response to your Edit 2:

Since your initial viewController was put into place programmatically, it isn't possible to unwind to the LoginViewController . In this case, put the landing @IBAction in your MainTabBarController and have it set the self.window?.rootViewController to the login screen.

You could try this it Worked for me.

[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];

I use it to dismiss a modal view so that I can return to the originating view.

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