How to destroy the current page when iOS uses UINavigationController to jump to the page

how iOS uses UINavigationController to jump to the page to destroy the current page.
is that I jump from page a to page b, and I want to destroy page a.
and how do I pass values to the page.

Thank you, bosses.

Dec.06,2021

one solution is that you don't have to destroy the current page, you just need to go back without going back to the current page.
NavigationController can be returned to rootController.

navigationController?.popToRootViewController(animated: true)

in addition, NavigationController can get all the View Controller in the current navigation stack. You can return to the page you specified. Or destroy any view controller in the stack.

navigationController?.viewControllers

if you pass a value, you should want to ask the forward value, right? There are two ways:

  1. write attribute operation to pass value when creating B Controller in A Controlelr
  2. if you use storyboard, you can pass a value by overriding the prepareForSegue:sender: method of A Controller

try a page removeFromParentViewController . There are many ways to pass values, depending on your specific needs


is to specify the jump position and skip one or more controllers in your stack. The core method is -(nullable UIViewController *) popViewControllerAnimated: (BOOL) animated; , just get the controller you want in your navigation.viewControllers. The following code can be referred to slightly. It is a self-written navigation base class, which implements manual control and location assignment of click and return

.
// .h
@property (nonatomic, assign) NSInteger targetIndex;
@property (nonatomic, copy) void(^backOperStandBy)(void(^handleBack)(BOOL back,NSInteger targetIndex),NSInteger operIndex);
// .m
- (void)sp_popViewController{
    
    if (self.backOperStandBy != nil) {
        __weak typeof(self) ws = self;
        self.backOperStandBy(^(BOOL back, NSInteger targetIndex) {
                if (back) {
                    ws.targetIndex = targetIndex;
                    [ws handlePopAction];
                }
        }, self.viewControllers.count-1);
    } else {
        [self handlePopAction];
    }
    
}

- (void)handlePopAction
{
    if (self.targetIndex == RootIndex) {
        [self popToRootViewControllerAnimated:YES];
        return;
    }
    
    if (self.targetIndex != 0 && self.targetIndex > 0) {
        UIViewController *target = self.viewControllers[self.targetIndex];
        self.targetIndex = 0;
        if (target) {
            [self popToViewController:target animated:YES];
        }else{
            [self popViewControllerAnimated:YES];
        }
        return;
    }
    // 
    [self popViewControllerAnimated:YES];
}

reference: https://github.com/rickytan/R.


generally speaking

self.navigationController?.setViewControllers([B_ViewController], animated: true)
Menu