简体   繁体   中英

iOS Controlling Back Button from Navigation Controller

I have a three views: 1) the initial on my Tab Bar 2) view segued from #1 3) view segued from #2

The normal use of the App is to start at #1, go to #2, then #3, which will always go back to #2. I would like the back button for #2 to ALWAYS go back to #1. However, after #3 has be entered and returned to #2, the back button goes to #3 (instead I want #1). I currently use push segues. Any ideas on how to update this?

use:

popToRootViewControllerAnimated:

instead of:

popViewControllerAnimated:

I ended up solving using this link Removing viewcontrollers from navigation stack and surrounding it with an if statement to detect at least 2 view controllers:

 - (void) viewWillDisappear:(BOOL)animated{
NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
int i = [navigationArray count];

// [navigationArray removeAllObjects];    // This is just for remove all view controller from navigation stack.
if (i > 1){
    NSLog(@"count: %d", i);
    [navigationArray removeObjectAtIndex: 1];  // You can pass your index here
    self.navigationController.viewControllers = navigationArray;
}
}

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