UITabBarController中的UINavigationController - 转到标签选择顶部

问题描述:

我有一个UITabBarController五个视图,其中一个视图中有一个UINavigationControllerUITabBarController中的UINavigationController - 转到标签选择顶部

我如何确保在选择包含UINavigationController的选项卡时它会转到UINavigationController的顶视图?

有没有UITabViewController类。我假设你的意思是UITabBarController

设置您的UITabBarController的代表。给委托人一个对导航控制器的参考。比如,也许你会使用你的应用程序委托作为标签栏控制器的委托:

@interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> 

@property (nonatomic, weak) IBOutlet UINavigationController *theNavigationController; 

确保您挂钩的财产,无论是在你的笔尖或代码。 然后,在委托,重写tabBarController:didSelectViewController:方法:

@implementation AppDelegate 

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 
    if (viewController == self.theNavigationController) { 
     [self.theNavigationController popToRootViewControllerAnimated:NO]; 
    } 
} 
+2

代替与基准进行比较,为什么不利用'[的viewController isKindOfClass:[UINavigationController的类]]检查选择的viewController的类型',然后类型转换并对结果执行'popToRootviewController'? – Till 2012-02-20 21:53:46

+0

哎呦,解决了我的头衔。 – Baub 2012-02-20 21:56:18

+1

@我给出了一个适合这个问题的答案。它有优点和缺点。 – 2012-02-20 22:05:29

设置UITabBarViewContoller的委托,并实现其tabBarController:didSelectViewController:,使其调用您的UINavigationControllerpopToRootViewControllerAnimated:当选择UINavigationController