iOS:Tabbar - 加载选项卡的默认状态

时间:2012-03-08 17:47:56

标签: ios xcode ipad uitabbarcontroller uitabbar

我需要一些想法或指出以下问题:

我有一个以TabBarView开头的应用程序 - 在某些标签中有不同的View / ViewControllers,它们通过seques连接。 如果更改了活动Tab,我希望(现在)打开Tab加载此Tab的“Start”-View / ViewController,而不是此Tab上最后一个活动的View / ViewController。 我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

我建议您使用UITabBarDelegate方法:tabBarController:didSelectViewController:

结合UINavigationController方法:popToRootViewControllerAnimated:

因此,当用户选择选项卡时,您可以确保导航从根控制器开始。

编辑回应评论:

这不是一个理想的情况,但您可以在app委托中引用UITabBarController。 E.g:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Get reference to Tab Bar Controller as the root view
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

    // Set Delegate
    tabBarController.delegate = self;

    return YES;
}

然后,您可以实现类似于:

的UITabBarDelegate方法
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    // Pop to root if the selected controller is a navigation controller.
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        [((UINavigationController *)viewController) popToRootViewControllerAnimated:NO];
    }
}

我还没有测试过这个!