Calling popViewControllerAnimated twice

前端 未结 4 1373
臣服心动
臣服心动 2021-01-30 11:26

I\'ve got a UINavigationController with a series of UIViewControllers on it. Under some circumstances, I want to pop back exactly two levels. I thoug

相关标签:
4条回答
  • 2021-01-30 11:41

    Also, as to what you were doing wrong, I believe the reason why [self.navigationController popViewControllerAnimated:YES] isn't working the second time is because you are probably making this second call on the screen that is being popped on the first call. After the first call, the current view is removed from the navigation controller, so by the time you make the second call, self.navigationController will return nil because it no longer has a navigation controller.

    0 讨论(0)
  • 2021-01-30 11:42

    It works for me if you save the reference to the UINavigationViewController and use the saved instance:

    UINavigationViewController* savedUinvc = self.navigationController;
    UIViewController* one = [savedUinvc  popViewControllerAnimated:YES];
    if (...) {
        // pop twice if we were doing XYZ
        UIViewController *two = [savedUinvc  popViewControllerAnimated:YES];
        // stored in "one" and "two" for debugging, "two" is always 0 here.
    }
    
    0 讨论(0)
  • 2021-01-30 11:52

    In this case you would need to pop back to a specific viewcontroller in the navigationController like so:

    [self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:2] animated:YES];
    

    That code would pop to the third viewcontroller on the navigationController's stack.

    0 讨论(0)
  • 2021-01-30 12:02

    I think its better to count the number of view controllers in you stack and then subtract the number of view controllers you would like to pop.

     NSInteger noOfViewControllers = [self.navigationController.viewControllers count];
     [self.navigationController 
     popToViewController:[self.navigationController.viewControllers 
     objectAtIndex:(noOfViewControllers-2)] animated:YES];
    

    With this solution you wont mess up the pop if you add a new view to your project later.

    0 讨论(0)
提交回复
热议问题