How can I pop a view from a UINavigationController and replace it with another in one operation?

后端 未结 16 2174
谎友^
谎友^ 2020-11-27 10:01

I have an application where I need to remove one view from the stack of a UINavigationController and replace it with another. The situation is that the first view creates an

16条回答
  •  [愿得一人]
    2020-11-27 10:34

    Alternatively,

    You can use category to avoid self.navigationController to be nil after popViewControllerAnimated

    just pop and push, it's easy to understand, don't need to access viewControllers....

    // UINavigationController+Helper.h
    @interface UINavigationController (Helper)
    
    - (UIViewController*) popThenPushViewController:(UIViewController *)viewController animated:(BOOL)animated;
    
    @end
    
    
    // UINavigationController+Helper.m
    @implementation UINavigationController (Helper)
    
    - (UIViewController*) popThenPushViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        UIViewController *v =[self popViewControllerAnimated:NO];
    
        [self pushViewController:viewController animated:animated];
    
        return v;
    }
    @end
    

    In your ViewController

    // #import "UINavigationController+Helper.h"
    // invoke in your code
    UIViewController *v= [[MyNewViewController alloc] init];
    
    [self.navigationController popThenPushViewController:v animated:YES];
    
    RELEASE_SAFELY(v);
    

提交回复
热议问题