Ankita
Ankita

Reputation: 479

How can i pop back in navigation controller

I have six viewControllers. On viewController1 I have a button which takes me to the viewController6.

Now from viewController6 I want to pop back to viewController5, viewController4, viewController3, viewController2 and viewController1 respectively.

How can I do this?

Upvotes: 2

Views: 1920

Answers (5)

user523234
user523234

Reputation: 14834

Use [self.navigationController pushViewController:] in this order: from ViewController1, 6, 5, 4, 3, 2.

And use [self.navigationController popToRootViewControllerAnimated:] to go from ViewController2 to 1.

Upvotes: 0

pradeepa
pradeepa

Reputation: 4164

Use either

popToViewController:animated
or
setViewControllers:animated

Upvotes: 0

Anusha Kottiyal
Anusha Kottiyal

Reputation: 3905

Try this,

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];

Insert your required view controllers in order using

[allViewControllers insertObject:viewControllerX atIndex:requiredIndex];

If you want to replace only previous viewController use,

 [allViewControllers replaceObjectAtIndex:indexRequired withObject:viewControlerX];

Then set back the navigation stack,

self.navigationController.viewControllers = allViewControllers;

Then you can use following to pop back to any viewController :

[self.navigationController popViewControllerAnimated:YES];

Upvotes: 3

Dixit Patel
Dixit Patel

Reputation: 3192

Yes this is Possible .

if you want to pop 1 ViewController from controller stack

[self.navigationController popViewControllerAnimated:YES];

go back to the fist ViewController.

[self.navigationController popToRootViewControllerAnimated:YES]; 

For More Information use this Link

http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

Upvotes: 0

Levi
Levi

Reputation: 7343

You can use [self.navigationController popViewControllerAnimated:YES]; if you want to pop just 1 ViewController, or use [self.navigationController popToRootViewControllerAnimated:YES]; to go back to the fist ViewController

Upvotes: 0

Related Questions