Jiahao Jiang
Jiahao Jiang

Reputation: 33

How can I go back to the first viewController from the third viewController

I entered the third viewController from the sencond viewController ,but i don't want to go back to the second viewController,I need to go back to my first viewController directly,how can I implement that.my english is poor,hope you can understand my question,thx

Upvotes: 1

Views: 767

Answers (5)

Sushil Sharma
Sushil Sharma

Reputation: 2361

Refer this answer https://stackoverflow.com/a/11027116/3883040 .Using isKindOfClass you can pop to the viewController from your navigation stack if the viewController is in navigation stack.

Upvotes: 0

Saheb Roy
Saheb Roy

Reputation: 5957

Try this -

Currently you are in your ThirdViewController, and say your FirstViewController's class name is "FVC".

NSArray *allViewControllers = [self.navigationController viewControllers];
for (UIViewController *aViewController in allViewControllers) {
   if ([aViewController isKindOfClass:[FVC class]]) {
       [self.navigationController popToViewController:aViewController  animated:YES];
   }
}

That should do the trick

EDIT -

As op wanted an explanation this is how it is.

In your Navigation stack there are currently - VC1, VC2, VC3 (you are @ VC3 now). Next when you call this method [self.navigationController popToViewController:vc1], your navigation stack has VC1, as you are popping from VC3 to VC1, thus popping out all the VC's in between, thus freeing your memory

Upvotes: 1

Avinash651
Avinash651

Reputation: 1399

Use this line of code -

[self.navigationController popToRootViewControllerAnimated:YES];

Or use this-

 NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
for (UIViewController *aViewController in allViewControllers) {
    if ([aViewController isKindOfClass:[YourStoryBoardName class]]) {
        [self.navigationController popToViewController:aViewController animated:YES];
    }
}

Upvotes: 0

matthias
matthias

Reputation: 947

There is a method for that

- (NSArray<__kindofUIViewController *> * _Nullable)popToRootViewControllerAnimated:(BOOL)animated

You'll need to do something like

[self.navigationController popToRootViewControllerAnimated:YES];

Refer to the Apple Docs: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/popToRootViewControllerAnimated:

Upvotes: 0

Garry
Garry

Reputation: 407

you can use navigation methods .

 [[self navigationController ] popToRootViewControllerAnimated:YES];

or you use this

popToViewController:(UIViewController *)viewController animated:(BOOL)animated;

& pass your controller in which you want to pop.

Upvotes: 0

Related Questions