简体   繁体   中英

How to set a tabBarController as rootViewController

In AppDelegate, I want to set a TabBarController as a rootViewController.

I have tried:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

I have also tried:

UITabBarController *tabBarController = [[UITabBarController alloc] init];
self.window.rootViewController = tabBarController;

But It says:

Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?

My full code in AppDelegate:

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

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    // Movies
    MediaListViewController *moviesVC = (MediaListViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MediaList"];
    moviesVC.title = @"Movies";
    moviesVC.tabBarItem.image = [[UIImage imageNamed:@"superman"] imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
    UINavigationController *moviesNC = [[UINavigationController alloc] initWithRootViewController:moviesVC];
    moviesNC.navigationBar.barTintColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    moviesNC.navigationBar.tintColor = [UIColor yellowColor];
    moviesNC.navigationBar.barStyle = UIBarStyleBlack;

    //DVDs
    MediaListViewController *dvdsVC = (MediaListViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MediaList"];
    dvdsVC.title = @"DVDs";
    dvdsVC.tabBarItem.image = [[UIImage imageNamed:@"hulk"] imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
    UINavigationController *dvdsNC = [[UINavigationController alloc] initWithRootViewController:dvdsVC];
    dvdsNC.navigationBar.barTintColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    dvdsNC.navigationBar.tintColor = [UIColor yellowColor];
    dvdsNC.navigationBar.barStyle = UIBarStyleBlack;

    tabBarController.viewControllers = @[moviesNC, dvdsNC];
    tabBarController.tabBar.barTintColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    [self.window makeKeyAndVisible];

    return YES;
}

There is a key in the info.plist which specifies the main storyboard file that need to be used in your application.

info.plist中

So whenever your app loads, iOS checks for this key and try to initialise the storyboard with name that is matching to the value of this key. For initialising a storyboard an entry-point (initial view controller) should be set. Even though you are setting the tab controller through code, the iOS system tries to initialise the storyboard and throws that message.

So for fixing the issue there is two options:

  1. Just set an initial view controller in your storyboard file (A dummy entry point), in app delegate you can override it (I recommend this approach)
  2. Just go ahead and delete the UIMainStoryboardFile aka Main storyboard file base name key from info.plist (This approach is simple and works, but you can never initialise your storyboard unless you set an entry point. So if you chose this option you can't never use storyboard for your design, you are limited to design your UI either using xib or via code)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM