美文网首页
判断页面返回时使用dismiss还是pop

判断页面返回时使用dismiss还是pop

作者: 雷霸龙 | 来源:发表于2021-03-29 21:56 被阅读0次

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

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

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

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

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

- (void)backAction {
    if ([self.navigationController.viewControllers.firstObject isEqual:self]) {
        //当前页面尚未被Push过
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)backAction {
    if ([self.navigationController.viewControllers indexOfObject:self] == 0) {
        //当前页面尚未被Push过
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

相关文章

网友评论

      本文标题:判断页面返回时使用dismiss还是pop

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