gpt4 book ai didi

ios - 如何将 tabBarController 设置为 rootViewController

转载 作者:行者123 更新时间:2023-12-01 20:04:28 25 4
gpt4 key购买 nike

在 AppDelegate 中,我想将 TabBarController 设置为 rootViewController。

我试过了:

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

我也试过:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
self.window.rootViewController = tabBarController;

但它说:

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



我在 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 中有一个键,它指定需要在您的应用程序中使用的主 Storyboard文件。

info.plist

因此,每当您的应用程序加载时,iOS 都会检查此键并尝试使用与此键的值匹配的名称来初始化 Storyboard 。为了初始化 Storyboard,应该设置一个入口点(初始 View Controller )。即使您通过代码设置选项卡 Controller ,iOS 系统也会尝试初始化 Storyboard 并抛出该消息。

因此,要解决此问题,有两种选择:

  • 只需在您的 Storyboard文件中设置一个初始 View Controller (一个虚拟入口点),在应用程序委托(delegate)中您可以覆盖它(我推荐这种方法)
  • 继续删除UIMainStoryboardFile又名主要 storyboard file base name来自 info.plist 的 key (这种方法简单且有效,但除非设置入口点,否则永远无法初始化 Storyboard 。因此,如果您选择此选项,则不能永远使用 Storyboard 进行设计,您只能使用 xib 或通过代码)
  • 关于ios - 如何将 tabBarController 设置为 rootViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39305867/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com