Ios 如何将tabBarController设置为rootViewController
在AppDelegate中,我想将TabBarController设置为rootViewController 我试过:Ios 如何将tabBarController设置为rootViewController,ios,objective-c,uitabbarcontroller,Ios,Objective C,Uitabbarcontroller,在AppDelegate中,我想将TabBarController设置为rootViewController 我试过: UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; 我也尝试过: UITabBarController *tabBarController = [[UITabBarController alloc] init]; self.window.roo
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
我也尝试过:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
self.window.rootViewController = tabBarController;
但它说:
未能实例化的默认视图控制器
UIMainstryBoardFile“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;
}
info.plist中有一个键,用于指定应用程序中需要使用的主情节提要文件 因此,每当加载应用程序时,iOS都会检查该键,并尝试使用与该键值匹配的名称初始化情节提要。要初始化故事板,应设置入口点(初始视图控制器)。即使您通过代码设置tab控制器,iOS系统也会尝试初始化情节提要并抛出该消息 因此,要解决这个问题,有两种选择:
info.plist
(这种方法简单有效,但除非设置入口点,否则您永远无法初始化情节提要。因此,如果您选择此选项,您永远无法在设计中使用情节提要,您只能使用xib或通过代码设计UI)第二次尝试时发生了什么?UITabBarController*tabBarController=[[UITabBarController alloc]init];您好@AnilVarghese,它说的和第一次一样。在main.storyboard中,您是否将第一个控制器(可能是tabbar控制器)设置为
initialViewController
谢谢@AnilVarghese,它解决了我的问题。