ios - 导航返回显示过渡期间的其他Viewcontroller

标签 ios objective-c uinavigationcontroller navigation

我有三个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但很快

enter image description here

关于ios - 导航返回显示过渡期间的其他Viewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48848383/

相关文章:

iphone - MPMediaPickerController.showsCloudItems 似乎什么都不做

iphone - 创建两个ios开发人员帐户

objective-c - 从 url 保存图像需要很长时间

ios - 如何在一个类中实现两个相同类型的委托(delegate)?

objective-c - 设置相对于设备尺寸的自动布局约束

iphone - CCRandom_0_1()生成相同的数字

ios - OTA安装:在设备上安装IPA进行测试

iphone - 如何从第二层向下建立 UINavigationController(不是从 Delegate/RootController 层)

ios - 使用 swift2 从登录/注册 ViewController 导航到 SWRevealViewController 的 sw_front ViewController

iphone - viewDidLoad 在 popViewController 调用后被调用吗?