我有三个Viewcontrollers:ViewControllerA,ViewControllerB和ViewControllerC。
当我在 ViewControllerC 时,点击导航返回按钮直接返回ViewControllerA,跳过 ViewControllerB 。
我尝试了以下方法,两者都有效。但是,我想知道从ViewController C转换到ViewController A时,它会在转换过程中一秒钟显示ViewController B.
有没有办法直接从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];
}
答案 0 :(得分:2)
您需要使用setViewControllers
方法,并且只传递viewControllerA,它是navigationController.viewControllers
数组中的第一个元素
<强>代码强>
- (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但快速
答案 1 :(得分:0)
试试popToRootViewControllerAnimated
。它将移动到NavigationController嵌入的First ViewController。
[self.navigationController popToRootViewControllerAnimated:YES];
答案 2 :(得分:0)
NSArray *arrayViewControllers = [self.navigationController viewControllers];
for (UIViewController *viewcontroller in arrayViewControllers) {
if ([viewcontroller isKindOfClass:[ViewControllerA class]]) {
[self.navigationController popToViewController:viewcontroller animated:true];
}
}