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

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

作者: 林希品 | 来源:发表于2023-09-12 09:44 被阅读0次

方法一:通过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];
    }
}

相关文章

网友评论

      本文标题:iOS开发_返回页面时使用dismiss还是pop?

      本文链接:https://www.haomeiwen.com/subject/uniyvdtx.html