iOS开发_返回页面时使用dismiss还是pop?

方法一:通过ViewController的属性presentingViewController判断当前页面是否是被present出的,来确定采用dismiss方法

- (void)backAction {
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

方法二:通过NavgationController的属性topViewController判断当前页面是否是被push出的最上层页面,来确定采用pop方法

- (void)backAction {
    if (self.navigationController.topViewController == self) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

方法三:通过NavgationController的属性viewcontrollers数组索引,来判断当前页面是否是被push过,来确定采用dismiss方法

- (void)backAction {
    if ([self.navigationController.viewControllers.firstObject isEqual:self]) {//当前页面尚未被Push过
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容