ios - 如何在导航 View Controller 中关闭整个 View Controller 堆栈?

标签 ios objective-c uiviewcontroller uinavigationcontroller

我有两个导航 View Controller 。当我单击 View Controller 中属于第二个导航 Controller 的按钮时,我想关闭该导航 Controller 的完整 View Controller 堆栈,并希望转到第一个导航 Controller 中的 View Controller 。我怎样才能做到这一点?我试过[self.navigationController dismissViewControllerAnimated:YES completion:nil];似乎什么也没有发生。如何做到这一点?

最佳答案

该错误必须在其他地方。您描述的您正在使用的代码确实有效。我创建了一个新项目并做了一个非常简单的例子:

#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

如果你把它复制到一个新项目中,ViewController是您的主视图 Controller ,导航 Controller 将被创建并呈现在 View 上。背景将是红色的。

通过按下屏幕的左上角,一个相同颜色的新 Controller 将被推送到当前顶部导航 Controller 。

通过按下右上角,一个新的导航 Controller 将呈现一个绿色的 View Controller 。

通过按下左下角,您可以弹出当前 View Controller (如果有)。

通过按右下角,您将关闭顶部导航 Controller 。

因此,您的情况是按左上角几次以在单个导航 Controller 上生成一堆 View Controller 。然后按右上角以显示另一个导航 Controller (绿色)。在左上角按几次以在绿色导航 Controller 上创建一堆 View Controller 。现在按右下角关闭整个绿色堆栈并返回红色导航 Controller 堆栈。

多检查一下您的代码,以了解您的情况发生了什么,为什么您会遇到问题。首先检查是否 self.navigationController是零。

关于ios - 如何在导航 View Controller 中关闭整个 View Controller 堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45499647/

相关文章:

objective-c - 您如何知道用户何时选择通过AirPlay播放视频?

iOS:UIVIew 在 segue 和 popViewController 之后回到起始位置

ios - UINavigtionController Nib 的顶部栏黑色不透明不在 UIViewController 上显示黑色

objective-c - 尝试访问 iOS 商店时出错

ios - Swift - UITableView 不放置 UIimage=nil

ios - 为什么用 CGImageSource 加载 gif 会导致内存泄漏?

iphone - 动态填充 UITableView

ios - SwiftEventBus注销swift

iphone - 如何在sqlite查询中传递表名

ios - 编写 UIView 层次结构