【问题标题】:Navigation Back shows other Viewcontroller during the transitionNavigation Back 在过渡期间显示其他 Viewcontroller
【发布时间】:2018-02-18 04:15:12
【问题描述】:

我有三个视图控制器:ViewControllerA、ViewControllerB 和 ViewControllerC。

当我在ViewControllerC时,点击导航Back按钮直接回到ViewControllerA,跳过ViewControllerB

我尝试了以下方法,它们都有效。但是,我想知道在从 ViewController C 过渡到 ViewController A 时,它会在一秒钟内显示 ViewController B 在过渡期间。

有没有办法直接从 ViewController C 导航到 ViewController A 跳过 ViewControllerB。

方法一:

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

【问题讨论】:

  • 你听说过 Unwind Segue 吗?如果您使用故事板,请通过this
  • [self.navigationController popToRootViewControllerAnimated:YES];
  • 你能显示你的后退按钮代码吗?
  • 我正在使用导航返回按钮项。
  • 不,抱歉,我现在就回答,这是一个常见问题

标签: ios objective-c uinavigationcontroller navigation


【解决方案1】:

您需要使用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,但很快

【讨论】:

  • 在从ViewControllerCViewControllerA 的转换过程中,该逻辑还显示ViewControllerB。我不想显示ViewControllerB
  • 我会复习然后给我一分钟@hotspring
  • 您创建了BarButtonItem 还是使用了导航返回按钮?
  • 你把逻辑直接放在backAction里了吗???如果你把逻辑放在viewWillDisappear 中会显示viewControllerB @hotspring
  • 你需要自己创建一个 barButtonItem @hotspring 并使用一个 backAction
【解决方案2】:

试试popToRootViewControllerAnimated。它将移动到 NavigationController 嵌入的 First ViewController。

[self.navigationController popToRootViewControllerAnimated:YES];

【讨论】:

  • 这并不能解决我的问题。在转换回rootViewcontroller 期间,它仍然显示所有视图控制器。
【解决方案3】:
        NSArray *arrayViewControllers = [self.navigationController viewControllers];


        for (UIViewController *viewcontroller in arrayViewControllers) {
            if ([viewcontroller isKindOfClass:[ViewControllerA class]]) {
                [self.navigationController popToViewController:viewcontroller animated:true];
            }
        }

【讨论】:

    猜你喜欢
    • 2020-05-02
    • 2017-11-18
    • 2011-07-16
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多