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

后端 未结 16 2096
谎友^
谎友^ 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:36

    If you want to show any other view controller by popToRootViewController then you need to do following:

             UIViewController *newVC = [[WelcomeScreenVC alloc] initWithNibName:@"WelcomeScreenVC" bundle:[NSBundle mainBundle]];
                NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
                [viewControllers removeAllObjects];
                [viewControllers addObject:newVC];
                [[self navigationController] setViewControllers:viewControllers animated:NO];
    

    Now, all your previous stack will be removed and new stack will be created with your required rootViewController.

    0 讨论(0)
  • 2020-11-27 10:37

    My favorite way to do it is with a category on UINavigationController. The following should work:

    UINavigationController+Helpers.h #import

    @interface UINavigationController (Helpers)
    
    - (UIViewController*) replaceTopViewControllerWithViewController: (UIViewController*) controller;
    
    @end
    

    UINavigationController+Helpers.m
    #import "UINavigationController+Helpers.h"

    @implementation UINavigationController (Helpers)
    
    - (UIViewController*) replaceTopViewControllerWithViewController: (UIViewController*) controller {
        UIViewController* topController = self.viewControllers.lastObject;
        [[topController retain] autorelease];
        UIViewController* poppedViewController = [self popViewControllerAnimated:NO];
        [self pushViewController:controller animated:NO];
        return poppedViewController;
    }
    
    @end
    

    Then from your view controller, you can replace the top view with a new by like this:

    [self.navigationController replaceTopViewControllerWithViewController: newController];
    
    0 讨论(0)
  • 2020-11-27 10:38

    You can check with navigation view controllers array which you give you all view controllers that you have added in navigation stack. By using that array you can back navigate to specific view controller.

    0 讨论(0)
  • 2020-11-27 10:40

    Not exactly the answer but might be of help in some scenarios (mine for example):

    If you need to pop viewcontroller C and go to B (out of stack) instead of A (the one bellow C), it's possible to push B before C, and have all 3 on the stack. By keeping the B push invisible, and by choosing whether to pop only C or C and B altogether, you can achieve the same effect.

    initial problem A -> C (I want to pop C and show B, out of stack)

    possible solution A -> B (pushed invisible) -> C (when I pop C, I choose to show B or also pop it)

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