Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pop Navigation Controller iOS

I have 3 navigation controllers. Each with many view controllers.

  • 1 NavigationController (modal Segue)-> 2 NavigationController (model Segue)-> 3 NavigationController

Now, how do you go from #3 NavigationController back to #1 NavigationController that I have been before? So I want

  • 1 NavigationController (modal Segue)-> 2 NavigationController (model Segue)-> 3 NavigationController (HOW???)-> 1 NavigationController

(To clarify, I would not want to go to a new 1 NavigationController. I want to go to the one that I used before.)

Help!

like image 806
coolcool1994 Avatar asked May 24 '14 05:05

coolcool1994


4 Answers

If you just want to dismiss the whole stack of 3 NavigationController, you can call this within any view controller in the 3

Objective C

[self.navigationController dismissViewControllerAnimated:YES completion:nil]

Swift 3, 4

self.navigationController?.dismiss(animated: true)

This will bring you back to the status before (model Segue)-> 3 NavigationController.

Maybe you can somehow call this in 2 before calling this in 3?

like image 91
John Pang Avatar answered Oct 31 '22 08:10

John Pang


[[self navigationController] popViewControllerAnimated:YES];
like image 43
Gaurav Gilani Avatar answered Oct 31 '22 07:10

Gaurav Gilani


In a view controller in navigationController1's stack, create an unwind @IBAction method:

Swift

@IBAction func unwindToMyViewController(_ segue: UIStoryboardSegue)

Objective-C

- (IBAction)unwindToMyViewController:(UIStoryboardSegue *)segue

In your storyboard, you can then hook up an unwind segue from a button in a view controller that is in the stack of navigationController3, by dragging from the button to the exit icon…

enter image description here

from there, select the unwind segue created above. When triggered, the segue will unwind ALL view controllers back to the view controller containing the unwind segue.

like image 42
Ashley Mills Avatar answered Oct 31 '22 07:10

Ashley Mills


Use that:

[self.**presentingViewController** dismissViewControllerAnimated:YES completion:nil];

instead of:

[self dismissViewControllerAnimated:YES completion:nil];

like image 40
Bary Levy Avatar answered Oct 31 '22 09:10

Bary Levy