网上找了一下,StackOverFlow的这个答案说,不应该将UITabBarControllier嵌入到UINavigationController中作为rootViewController,但是,我们的确想要这样做,所以只好寻找其它办法。不过,至少我们可以确定的是,问题出在rootViewController同时包含UITabBarController和UINavigationController。
几经尝试,最后发现,在设置为window.rootViewController之前,先指定tabBarController.selectedIndex = 0,问题解决。
可以得出,出现上述错误,是因为XCode不知道你需要push哪个子viewController,在加载navigationController的时候,不知道要载入哪一个controller,于是无脑的将tabBarController的viewControllers都动画载入了。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UIStoryboard *pStoryBoard = [UIStoryboard storyboardWithName:@"FirstStoryBoard" bundle:nil];
UINavigationController *pNavigationController = [pStoryBoard instantiateInitialViewController];
UITabBarController *pTabBarController = (UITabBarController *)pNavigationController.visibleViewController;
pTabBarController.selectedIndex = 0; /// 就是这句话
[UIApplication sharedApplication].delegate.window.rootViewController = pNavigationController; /// 这句话在其他VC也可以用
[self.window makeKeyAndVisible];
return YES;
}
附storyboard构建为:
新建storyboard,拖入NavigationController和TabBarController,segue链接为root view。
浙公网安备 33010602011771号