方法一:通過ViewController的屬性presentingViewController判斷當(dāng)前頁面是否是被present出的,來確定采用dismiss方法
- (void)backAction {
if (self.presentingViewController) {
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[self.navigationController popViewControllerAnimated:YES];
}
}
方法二:通過NavgationController的屬性topViewController判斷當(dāng)前頁面是否是被push出的最上層頁面,來確定采用pop方法
- (void)backAction {
if (self.navigationController.topViewController == self) {
[self.navigationController popViewControllerAnimated:YES];
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
方法三:通過NavgationController的屬性viewcontrollers數(shù)組索引蝌戒,來判斷當(dāng)前頁面是否是被push過,來確定采用dismiss方法
- (void)backAction {
if ([self.navigationController.viewControllers.firstObject isEqual:self]) {//當(dāng)前頁面尚未被Push過
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[self.navigationController popViewControllerAnimated:YES];
}
}