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];
}
}