如何在AppDelegate中为导航视图控制器和tabbar控制器设置rootViewController

时间:2018-01-26 05:34:42

标签: ios objective-c

我有这个问题。当我为TabbarController设置rootViewController时,它显示正确。但是我为导航栏设置了另一个rootViewController,TabbarController将无法显示。有什么想法吗?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.rootViewController = [[DCTabBarController alloc] init];
    DCTabBarController *tabBar = (DCTabBarController *)self.window.rootViewController;
    [tabBar setSelectedIndex:2];

    Map_ViewController *vc = [[Map_ViewController alloc] init];

    UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:vc];
    [rootNav.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbarBackImage"] forBarMetrics:UIBarMetricsDefault];
    rootNav.navigationBar.tintColor = [UIColor whiteColor];
    [rootNav.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];

    LeftViewController *leftVC = [[LeftViewController alloc] init];

    RightViewController *rightVC = [[RightViewController alloc] init];

    XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:rootNav];

    slideMenu.leftViewController = leftVC;
    slideMenu.rightViewController = rightVC;
    self.window.rootViewController = slideMenu;

    [self.window makeKeyAndVisible];

    return YES;
}

应用Adeel解决方案后,这里是输出。但是tabbar中的项目不会相应显示。 enter image description here

1 个答案:

答案 0 :(得分:1)

这里要提到的一件重要事情是,应用程序的窗口只能有一个rootViewController(当然)。就像我在评论中所说,你可能想做这样的事情。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    Map_ViewController *vc = [[Map_ViewController alloc] init];

    UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:vc];
    [rootNav.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbarBackImage"] forBarMetrics:UIBarMetricsDefault];
    rootNav.navigationBar.tintColor = [UIColor whiteColor];
    [rootNav.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];

    DCTabBarController *tabBar = [[DCTabBarController alloc] init];
    [tabBar setViewControllers:@[rootNav]];
    self.window.rootViewController = tabBar;

    LeftViewController *leftVC = [[LeftViewController alloc] init];

    RightViewController *rightVC = [[RightViewController alloc] init];

    XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:tabBar];

    slideMenu.leftViewController = leftVC;
    slideMenu.rightViewController = rightVC;
    self.window.rootViewController = slideMenu;

    [self.window makeKeyAndVisible];

    return YES;
}