我本來試用方法:
- (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饵骨,都只進(jìn)判斷1,不進(jìn)判斷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的最后一個(gè)是否是當(dāng)前控制器砂心,或者self.navigationController.topViewController == self
- (IBAction)dismiss:(id)sender {
if (self.navigationController.topViewController == self) {
[self.navigationController popViewControllerAnimated:YES];
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}