2012-03-23 152 views
0

我查看了建议列表,没有看到任何似乎符合我的困境的东西。故事板:更改故事板中指定的UINavigation控制器的根视图

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

其中一个导航堆栈实际上是3个视图控制器的“环”。我用一个自定义的segue用一个简单的替换(我把一个视图放在一个视图上)来代替通常的导航堆栈的“堆叠”。

这很奇妙。故事板和自定义segue正是医生所订购的。

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

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

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

+0

好的。我想我已经拥有了。这有点迂回,但UIStoryboard :: instantiateViewControllerWithIdentifier方法是我所需要的。我只需要得到适当的故事板就位。当我让它满意时,我会发布代码。 – 2012-03-23 22:07:25

回答

0

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

好的。我想到了。

而不是取代根,我只需推开适当的控制器。这是因为更改根目录需要重新实例化控制器,而狐狸只是不值得追逐。这对UX没有影响,所以我们这样做:

/**************************************************************//** 
\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]; 
       } 
      } 
     } 
}