解决方案是将 UINavigationController 子类化,并将您的子类与 UITabBarController 一起使用。我加入了其他几个有用的功能。
这样做很好 - 我的应用程序有 5 颗星,没有人抱怨它:
@implementation MyNavigationController
// This suppresses the normal pop to the root view controller
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
{
return @[];
}
// Extra: give my base classes some notice this is going to happen
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
UIViewController *vc = self.topViewController;
if ([vc respondsToSelector:@selector(viewControllerWillBePopped)]) {
[vc performSelector:@selector(viewControllerWillBePopped)];
}
return [super popViewControllerAnimated:animated];
}
// Extra: let the UIViewController refuse to pop
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
BOOL ret = YES;
UIViewController *vc;
for(UIViewController *obj in self.viewControllers) {
if(obj.navigationItem == item) {
vc = obj;
break;
}
}
if ([vc respondsToSelector:@selector(shouldPop)]) {
NSNumber *retVal = [vc performSelector:@selector(shouldPop)];
ret = [retVal boolValue];
if(!ret) return NO;
}
return [super navigationBar:navigationBar shouldPopItem:item];
}
@end