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

2023-09-12  本文已影响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];
    }
}
上一篇下一篇

猜你喜欢

热点阅读