简体   繁体   中英

How to dismiss a whole stack of view controllers in navigation view controller?

I have two navigation view controllers.When I click on a button in a ViewController that belongs to the second navigation controller i want to dismiss the complete view controller stack of that navigation controller and want to go to a view controller in the first navigation controller.How can I do this? I tried [self.navigationController dismissViewControllerAnimated:YES completion:nil]; and nothing seems to happen.How to do this ?

The bug must be somewhere else. The code you described you are using does indeed work. I created a new project and made an extremely simple example:

#import "ViewController.h"

@interface MyViewController : UIViewController
- (instancetype)initWithColor:(UIColor *)color;
@end


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:[[MyViewController alloc] initWithColor:[UIColor redColor]]] animated:YES completion:nil];
}

@end



@implementation MyViewController

- (instancetype)initWithColor:(UIColor *)color {
    if((self = [super init])) {
        self.view.backgroundColor = color;
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [touches.anyObject locationInView:nil];
    if(point.x < self.view.frame.size.width*0.5 && point.y < self.view.frame.size.width*0.5) {
        [self.navigationController pushViewController:[[MyViewController alloc] initWithColor:self.view.backgroundColor] animated:YES];
    } else if(point.x > self.view.frame.size.width*0.5 && point.y < self.view.frame.size.width*0.5) {
        [self.navigationController presentViewController:[[UINavigationController alloc] initWithRootViewController:[[MyViewController alloc] initWithColor:[UIColor greenColor]]] animated:YES completion:nil];
    } else if(point.x < self.view.frame.size.width*0.5 && point.y > self.view.frame.size.width*0.5) {
        [self.navigationController popViewControllerAnimated:true];
    } else if(point.x > self.view.frame.size.width*0.5 && point.y > self.view.frame.size.width*0.5) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

@end

If you copy this into a new project where ViewController is your main view controller a navigation controller will be created and presented on view did appear. The background will be red.

By pressing the top-left part of the screen a new controller of the same color will be pushed to the current top navigation controller.

By pressing top right a new navigation controller will be presented with a green view controller.

By pressing bottom left you may pop the current view controller if any.

And by pressing bottom right you will dismiss the top navigation controller.

So your case is pressing top-left a few times to generate a stack of view controllers on a single navigation controller. Then press top right to present yet another navigation controller (green one). Press a few times on top left to create a stack of few view controllers on a green navigation controller. Now press the bottom right to dismiss the whole green stack and be back to the red navigation controller stack.

Check your code a bit more to see what is going on in your case, why you are experiencing issues in your case. First check if self.navigationController is nil.

试试这个,我认为它对你有用

self.navigationController?.popToRootViewController(animated: true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM