coolcool1994
coolcool1994

Reputation: 3804

How to pop Navigation Controller iOS

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

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

(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!

Upvotes: 10

Views: 18158

Answers (6)

John Pang
John Pang

Reputation: 2503

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?

Upvotes: 8

Hamid Zandi
Hamid Zandi

Reputation: 2902

this code will pop a navigation controller with all view controllers in it

// pop root view controller
UIViewController *rootViewController = [self.navigationController viewControllers][0];
[rootViewController dismissViewControllerAnimated:YES completion:nil];

so you can do something like this:

// pop navigationController3 without animation
UIViewController *rootViewController3 = [navigationController3 viewControllers][0];
[rootViewController3 dismissViewControllerAnimated:NO completion:nil];

// pop navigationController2 with animation
UIViewController *rootViewController2 = [navigationController2 viewControllers][0];
[rootViewController2 dismissViewControllerAnimated:YES completion:nil];

Upvotes: 0

Bala Prabu
Bala Prabu

Reputation: 11

With help of this, You can get the 1 Nav controll :-

[(UINavigationController *)self.view.window.rootViewController popViewControllerAnimated:YES];

Upvotes: 0

Ashley Mills
Ashley Mills

Reputation: 53092

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.

Upvotes: 1

Bary Levy
Bary Levy

Reputation: 624

Use that:

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

instead of:

[self dismissViewControllerAnimated:YES completion:nil];

Upvotes: 1

Gaurav Gilani
Gaurav Gilani

Reputation: 1584

[[self navigationController] popViewControllerAnimated:YES];

Upvotes: 22

Related Questions