故事板:更改故事板中指定的导航控制器的根视图

时间:2012-03-23 20:10:59

标签: iphone ios uinavigationcontroller storyboard nib

我查看了建议的清单,并没有看到任何似乎适合我的困境。

我正在写我的第一个故事板项目。我有一个管理4个选项卡的UITabBarController。每个选项卡都有一个UINavigationController作为其根目录,每个选项卡都包含许多视图。

其中一个导航堆栈实际上是3个视图控制器的“环”。我使用自定义segue来替换导航堆栈的常规“堆积”,只需更换一个(我打开一个视图)。

这非常有用。故事板和自定义区域正是医生所要求的。

但是,我想让用户选择他们开始的3个视图中的哪一个(成为导航堆栈的根目录)。故事板坚持我选择其中一个作为根,所以我这样做。但是,在运行时,用户可能希望初始视图不同,因此我希望导航堆栈具有不同的视图。

在基于nib的应用程序中,我只是从一个nib实例化一个新的控制器,并用该控制器替换当前的root。

如何选择控制器并在故事板中执行此操作?

1 个答案:

答案 0 :(得分:0)

更新: 感谢评论in another thread,我能够简化这一点。

行。我想通了。

我只需按下相应的控制器,而不是替换根。这是因为更改根需要重新实例化控制器,而狐狸不值得追逐。这对用户体验没有任何影响,所以我们这样做:

/**************************************************************//**
 \brief Selects the initial search screen, depending on the user's choice.
 *****************************************************************/
- (void)selectInitialSearchAndForce:(BOOL)force         ///< If YES, then the screen will be set to the default, even if we were already set to one.
{
    UITabBarController  *tabController = (UITabBarController *)self.window.rootViewController;

    [tabController setSelectedIndex:0]; // Set the tab bar to the search screens.

    if ( force )
        {
        UIStoryboard    *st = [tabController storyboard];

        [[searchNavController navigationController] popToRootViewControllerAnimated:NO];

        UIViewController    *newSearch = nil;

        switch ( [[BMLT_Prefs getBMLT_Prefs] searchTypePref] )
            {
            case _PREFER_MAP_SEARCH:
            if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPad)   // The map and simple searches are combined, on the iPad.
                {
                newSearch = [st instantiateViewControllerWithIdentifier:@"map-search"];
                }
            break;

            case _PREFER_ADVANCED_SEARCH:
            newSearch = [st instantiateViewControllerWithIdentifier:@"advanced-search"];
           break;
            }

        if ( newSearch )    // We push the controller, as opposed to changing the root, because it's easier, and doesn't violate any of our ground rules.
            {
            [[searchNavController navigationController] pushViewController:newSearch animated:NO];
            // In the case of the iPad, we use a standard navbar push, so we need to prime the simple view to properly populate the Advanced view back button.
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
                {
                UIViewController    *simpleViewController = [[[searchNavController navigationController] viewControllers] objectAtIndex:0];

                simpleViewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString([[simpleViewController navigationItem] title], nil)
                                                                                                         style:UIBarButtonItemStyleBordered
                                                                                                        target:nil
                                                                                                        action:nil];
                }
            }
        }
}