我有三个Viewcontroller:ViewControllerA,ViewControllerB和ViewControllerC。
当我在 ViewControllerC 中时,我单击Navigation Back 按钮直接返回ViewControllerA,而跳过 ViewControllerB 。
我尝试了以下方法,它们都有效。但是,我想知道从ViewController C过渡到ViewController A时,它在过渡期间在一秒钟内显示了ViewControllerB。
有没有一种方法可以直接从ViewController C导航到ViewController A,从而跳过ViewControllerB。
方法1:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
NSLog(@"back button pressed");
[self.navigationController popViewControllerAnimated:YES];
}
[super viewWillDisappear:animated];
}
方法2:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
NSLog(@"back button pressed");
//[self.navigationController popViewControllerAnimated:YES];
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
for (UIViewController *aViewController in allViewControllers) {
if ([aViewController isKindOfClass:[ViewControllerA class]]) {
[self.navigationController popToViewController:aViewController animated:NO];
}
}
}
[super viewWillDisappear:animated];
}
最佳答案
您需要使用setViewControllers
方法,并仅传递作为navigationController.viewControllers
数组中第一个元素的viewControllerA
代码
- (IBAction)backAction:(id)sender {
UIViewController * viewControllerA = [self.navigationController.viewControllers firstObject]; //we get the first viewController here
[self.navigationController setViewControllers:@[viewControllerA] animated:YES];
}
类似的答案在这里How to start from a non-initial NavigationController scene但很快
关于ios - 导航返回显示过渡期间的其他Viewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48848383/