使用视图层次结构在Appdelegate中设置根视图

时间:2014-12-25 08:16:34

标签: objective-c uiviewcontroller

我希望能够在app delegate中手动设置根视图控制器,并使用它的视图层次结构

我的视图控制器层次结构

  • TabBarController
    • 的UINavigationController
      • FirstViewController
        • DetailedViewController (我想显示此视图)

目前我所做的是

  DetailedViewController *vc = (DetailedViewController *)[mainStoryBoard instantiateViewControllerWithIdentifier:@"DetailedViewController"];
    [self.window setRootViewController:vc];
    [vc performSelector:@selector(setup:) withObject:section];

显然忽略了层次结构,因此当应用加载时,我没有导航栏或标签栏。

所以我试过这个

    UITabBarController *tabBarController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"tabController"];
    tabBarController.selectedIndex = 0;

    DetailedViewController *vc = (DetailedViewController *)[mainStoryBoard instantiateViewControllerWithIdentifier:@"DetailedViewController"];

    UINavigationController *navController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"NavController"];
    [navController pushViewController:vc animated:YES];

    [self.window setRootViewController:tabBarController];

但是使用这段代码我可以查看 FirstViewController 而不是 DetailedViewController

1 个答案:

答案 0 :(得分:0)

你的观点是这样的 你的tabbar有一个navview,其rootview是firstviewcontroller所以这段代码将firstview添加为rootView

 UITabBarController *tabBarController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"tabController"];
    tabBarController.selectedIndex = 0;

FrstViewController *firstView = (FirstViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"FirstViewController"];

    UINavigationController *navController = [UINavigationViewController alloc] initWithRootViewController:firstview]
    self.tabbarController.viewControllers = [NSArray arrayWithObjects:navController,nil]

    [self.window setRootViewController:tabBarController];

当我们第一次加载 viewDidload 时,推送detailviewcontroller

-(void) viewDidLoad
{
DetailedViewController *vc = (DetailedViewController *)[mainStoryBoard instantiateViewControllerWithIdentifier:@"DetailedViewController"];
[self.navigationController pushViewController vc];
}