【发布时间】:2016-09-03 10:26:15
【问题描述】:
在 AppDelegate 中,我想将 TabBarController 设置为 rootViewController。
我试过了:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
我也试过了:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
self.window.rootViewController = tabBarController;
但它说:
无法实例化默认视图控制器 UIMainStoryboardFile 'Main' - 也许指定的入口点是 没有设置?
我在 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;
}
【问题讨论】:
-
第二次尝试时发生了什么? UITabBarController *tabBarController = [[UITabBarController alloc] init];
-
嗨@AnilVarghese,它也说和第一个一样。
-
在您的 main.storyboard 中,您是否将第一个控制器(可能是标签栏控制器)设置为
initialViewController -
感谢@AnilVarghese,它解决了我的问题。
标签: ios objective-c uitabbarcontroller