我需要一些想法或起点来解决以下问题:

我有一个以TabBarView开头的应用程序-在某些选项卡中,有不同的View / ViewControllers通过定序连接。
如果活动的选项卡被更改,我希望(现在)打开的选项卡加载该选项卡的“开始” -View / ViewController,而不是该选项卡上最后一个活动的View / ViewController。
我怎样才能做到这一点?

最佳答案

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

结合UINavigationController方法:popToRootViewControllerAnimated:

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

编辑以回应评论:

这不是理想的情况,但是您可以在应用程序委托中引用UITabBarController。例如。:

- (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];
    }
}


我还没有测试过!

关于ios - iOS:Tabbar-加载选项卡的默认状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9622293/

10-14 21:45
查看更多