由于presentviewcontroller的方式弹出的viewcontroller不会被添加到self.navigationController.viewControllers数组中,而通过push方式显示的viewcontroller会存在该数组的最后,所以可以通过遍历navigationController的子控制器来判断push还是modal:
- (BOOL)isPresent {
BOOL isPresent;
NSArray *viewcontrollers = self.navigationController.viewControllers;
if (viewcontrollers.count > 1) {
if ([viewcontrollers objectAtIndex:viewcontrollers.count - 1] == self) {
isPresent = NO; //push方式
}
}
else{
isPresent = YES; // modal方式
}
return isPresent;
}
当需要自定义导航返回按钮时,可进行判断是pop还是dismiss
- (void)backCompletionHandle:(nullable void(^)())block {
if ([self isPresent]) {
[self dismissViewControllerAnimated:YES completion:^{
if (block) {
block();
}
}];
}else {
[self.navigationController popViewControllerAnimated:YES];
if (block) {
block();
}
}
}