简体   繁体   English

iOS通过导航控制器控制后退按钮

[英]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 我有三个视图:1)标签栏上的初始视图2)从#1锁定的视图3)从#2锁定的视图

The normal use of the App is to start at #1, go to #2, then #3, which will always go back to #2. 该应用程序的正常使用是从#1开始,转到#2,然后是#3,后者将始终回到#2。 I would like the back button for #2 to ALWAYS go back to #1. 我希望#2的返回按钮始终回到#1。 However, after #3 has be entered and returned to #2, the back button goes to #3 (instead I want #1). 但是,在输入#3并返回到#2之后,后退按钮将转到#3(而不是我想要的是#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: 我最终使用此链接解决了问题, 从导航堆栈中删除了视图控制器,并使用if语句将其包围,以检测至少2个视图控制器:

 - (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;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM