Adding a TabBarController programmatically
Asked Answered
D

3

6

I want to make a tab bar controller and navigation controller programmatically. My code works so far that it shows a tab bar at the bottom, but the OptionViewController doesn't say anything (no title) on the button of the second tab bar. The funny thing is, when i click the button without anything on it, the title appears (and so is his view), can someone explain to me what i am doing wrong? I tried to use the following code:

- (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;
}
Debrahdebrecen answered 6/9, 2011 at 11:51 Comment(4)
I think you need to add UINavigationController as a sub view in the Tab Bar controller with the controlling classes super class as UINavigationControllerPlagiarism
the problem is only the missing title, right? where are you setting the title of your OptionsViewConbtroller? If you are setting the title not in your init-method then the TabBarController only reads an empty title from your OptionsVC. I guess you are setting the title-property in sth. like viewDidLoad ?Insuppressible
I guess not, because this: [tbc.view addSubview:ovc_nc.view]; makes the screen completly empty!Debrahdebrecen
@ Thomas, i set the title indeed in the ViewDidLoad. As i set the funny thing is that the title suddenly appears when I click on the tabbar item!Debrahdebrecen
F
10

You need to set the tabBarItem and title of the UINavigationController and not its root 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;
}
Fireboard answered 6/9, 2011 at 11:55 Comment(4)
I've update my post, it will show you how to set the tabBarItem.Fireboard
Now it shows the title correctly, but now when I set the title in the optionsview on "Options" and with your method I init it on "Option", so when the application boots up I see "Default" and "Option". When I click on the TabBar item it changes title to what i configured in viewDidLoad with self.title!Debrahdebrecen
Don't set the title in viewDidLoad.Fireboard
Haha, then my navigation bar dont have any title! i changed it from setting self.title to self.navigationItem.title = @"Options"; ! Thank youDebrahdebrecen
L
3

I created the UITabbarController as rooview controller of the app with UINavigationController for UIViewController.

here one more example: I used xibs for View Controllers.

AppDelegate.m

I create a method name: 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];


}

It is texted in Xcode 9 with iOS 11.

Lindalindahl answered 30/9, 2017 at 7:27 Comment(0)
U
0

If someone needs a SWIFT version. This worked for me. Thanks @rckoenes for the objC answer I used to translate this from.

    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
Ununa answered 19/3, 2016 at 1:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.