程序员社区

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

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];
    }
}
赞(0) 打赏
未经允许不得转载:IDEA激活码 » 判断页面返回时使用dismiss还是pop

一个分享Java & Python知识的社区