簡體   English   中英

以編程方式添加TabBarController

[英]Adding a TabBarController programmatically

我想以編程方式制作標簽欄控制器和導航控制器。 我的代碼到目前為止工作,它在底部顯示一個標簽欄,但OptionViewController在第二個標簽欄的按鈕上沒有說任何內容(沒有標題)。 有趣的是,當我點擊按鈕時沒有任何東西,標題出現(他的觀點也是如此),有人可以向我解釋我做錯了什么嗎? 我試着使用以下代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2];

    DefaultViewController *dvc = [[DefaultViewController alloc] init];
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc];
    [tabItems addObject:dvc_nc];
    [dvc release];
    [dvc_nc release];

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc];
    [tabItems addObject:ovc_nc];
    [ovc release];
    [ovc_nc release];

    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = tabItems;
    self.tabController = tbc;
    [tabItems release];
    [tbc release];

    [self.window addSubview:self.tabController.view];

    return YES;
}

您需要設置UINavigationController的tabBarItem和標題,而不是它的根viewController。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2];

    DefaultViewController *dvc = [[DefaultViewController alloc] init];
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc];
    dvc_nc.tabBarItem.title = @"Default";
    dvc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"]];
    [tabItems addObject:dvc_nc];
    [dvc release];
    [dvc_nc release];

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc];
    ovc_nc.tabBarItem.title = @"Option"
    ovc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Optiomn" ofType:@"png"]];

    [tabItems addObject:ovc_nc];
    [ovc release];
    [ovc_nc release];

    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = tabItems;
    self.tabController = tbc;
    [tabItems release];
    [tbc release];

    [self.window addSubview:self.tabController.view];

    return YES;
}

我使用UINavigationController為UIViewController創建了UITabbarController作為應用程序的rooview控制器。

這里再舉一個例子:我為視覺控制器使用了xibs。

AppDelegate.m

我創建了一個方法名稱: setupAppHome

#pragma mark - SETUP HOME
-(void) setupAppHome{
    NSLog(@"set up the nano home");

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    if (_chatViewController == nil) {
        _chatViewController = [[ChatViewController alloc] initWithNibName:@"ChatViewController" bundle:nil];
        chatNav = [[UINavigationController alloc] initWithRootViewController:_chatViewController];
        chatNav.tabBarItem.title=@"Chat";
        chatNav.tabBarItem.image=[UIImage imageNamed:@"chat_icon.png"];

    }
    if (_callController == nil) {
        _callController = [[CallViewController alloc] initWithNibName:@"CallViewController" bundle:nil];
        callNav = [[UINavigationController alloc] initWithRootViewController:_callController];
        callNav.tabBarItem.title=@"Call";
        callNav.tabBarItem.image=[UIImage imageNamed:@"call_icon.png"];

    }
    if (_contanctsController == nil) {
        _contanctsController = [[ContactsViewController alloc] initWithNibName:@"ContactsViewController" bundle:nil];
        conNav = [[UINavigationController alloc] initWithRootViewController:_contanctsController];
        conNav.tabBarItem.title=@"Contact";
        conNav.tabBarItem.image=[UIImage imageNamed:@"contact_icon.png"];

    }
    if (_settingController == nil) {
        _settingController = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
        settingNav = [[UINavigationController alloc] initWithRootViewController:_settingController];
        settingNav.tabBarItem.title=@"Setting";
        settingNav.tabBarItem.image=[UIImage imageNamed:@"setting_icon.png"];

    }

    self.tabController = [[UITabBarController alloc] init];

    NSMutableArray          *controllers = [[NSMutableArray alloc] initWithCapacity:4];
    [controllers addObject:chatNav];
    [controllers addObject:callNav];
    [controllers addObject:conNav];
    [controllers addObject:settingNav];


    self.tabController.viewControllers = controllers;//@[chatNav,callNav,conNav,settingNav];

    self.tabController.selectedIndex=0;



    [self.window setRootViewController:self.tabController];
    [self.window makeKeyAndVisible];


}

它使用iOS 11在Xcode 9中發短信。

如果有人需要SWIFT版本。 這對我有用。 感謝@rckoenes的objC答案,我曾經將其翻譯過來。

    window?.makeKeyAndVisible()

    let dvc = HomeViewController()
    let dvc_nc = UINavigationController(rootViewController: dvc)
        dvc_nc.tabBarItem.title = "Home"
        dvc_nc.tabBarItem.image = UIImage(named: "HomeIcon")
    controllers.append(dvc_nc)

    let ovc = ProfileViewController()
    let ovc_nc = UINavigationController(rootViewController: ovc)
        ovc_nc.tabBarItem.title = "Profile"
        ovc_nc.tabBarItem.image = UIImage(named: "ProfileIcon")
    controllers.append(ovc_nc)

    let tbc = UITabBarController()
        tbc.viewControllers = controllers

    window?.rootViewController = tbc

    UINavigationBar.appearance().tintColor = UIColor(red: 0.05, green: 0.47, blue: 0.91, alpha: 1.0)
    UINavigationBar.appearance().barTintColor = UIColor(red: 0.05, green: 0.47, blue: 0.91, alpha: 1.0)
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent

暫無
暫無

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

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