簡體   English   中英

如何將tabBarController設置為rootViewController

[英]How to set a tabBarController as rootViewController

在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;
}

info.plist中有一個密鑰,用於指定應用程序中需要使用的主故事板文件。

info.plist中

因此,每當您的應用加載時,iOS都會檢查該鍵,並嘗試使用與該鍵的值匹配的名稱來初始化情節提要。 為了初始化情節提要,應設置一個入口點(初始視圖控制器)。 即使您通過代碼設置選項卡控制器,iOS系統也會嘗試初始化情節提要並引發該消息。

因此,要解決此問題,有兩種選擇:

  1. 只需在情節提要板文件(虛擬入口點)中設置初始視圖控制器,即可在應用程序委托中覆蓋它(我建議采用這種方法)
  2. 只需繼續並從info.plist刪除UIMainStoryboardFile又是Main storyboard file base name鍵(此方法很簡單且UIMainStoryboardFile ,但是除非設置入口點,否則您無法初始化Storyboard。因此,如果選擇此選項,則無法絕不使用情節提要進行設計,只能使用xib或通過代碼來設計UI)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM