在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;
}
答案 0 :(得分:2)
info.plist中有一个键,用于指定需要在应用程序中使用的主要故事板文件。
因此,只要您的应用加载,iOS就会检查此密钥并尝试使用与此密钥值匹配的名称初始化故事板。对于初始化故事板,应设置入口点(初始视图控制器)。即使您通过代码设置选项卡控制器,iOS系统也会尝试初始化故事板并抛出该消息。
因此,为了解决问题,有两种选择:
UIMainStoryboardFile
删除storyboard file base name
又名主info.plist
密钥(此方法很简单且有效,但除非设置入口点,否则无法初始化故事板。因此,如果您选择此选项,则永远不会将故事板用于您的设计,您只能使用xib或通过代码设计UI。