我本來試用方法:
- (IBAction)dismiss:(id)sender {
if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
//判斷1
[self dismissViewControllerAnimated:YES completion:nil];
} else if ([self.navigationController respondsToSelector:@selector(popViewControllerAnimated:)]) {
//判斷2
[self.navigationController popViewControllerAnimated:YES];
}
}
結(jié)果發(fā)現(xiàn)不管是push或present,都只進判斷1,不進判斷2.
于是google,
方法一:
通過判斷self有沒有present方式顯示的父視圖presentingViewController
- (IBAction)dismiss:(id)sender {
if (self.presentingViewController) {
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[self.navigationController popViewControllerAnimated:YES];
}
}
方法二:
通過判斷self.navigationController.viewControllers的最后一個是否是當前控制器,或者self.navigationController.topViewController == self
- (IBAction)dismiss:(id)sender {
if (self.navigationController.topViewController == self) {
[self.navigationController popViewControllerAnimated:YES];
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}