Search code examples
iphoneobjective-cioscocoa-touchuitabbar

UITabBar Title Not Showing


I've implemented a UITabBar programmatically exactly the way Apple recommends, and I can't get the title to show up. Here is how I'm implementing it:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{   
    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    MainViewController *mainViewController = [[MainViewController alloc] init];
    UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController: mainViewController];
    [[mainNavigationController navigationBar] setBarStyle: UIBarStyleBlack];

    [tabBarController setViewControllers: [NSArray arrayWithObjects: mainNavigationController, nil]];

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

    [mainViewController release];
    [mainNavigationController release];

    [tabBarController release];

    return YES;
}

Here is the init method of mainViewController where I add the title:

@implementation MainViewController

    - (id) init
    {
        self = [super initWithNibName: nil bundle: nil];

        if (self)
        {
          UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Main" image: nil tag: 1];
          [self setTabBarItem: tabBarItem];
          [tabBarItem release];
        }

        return self;
    }

I noticed there are a bunch of other questions on this, and some have hacky type solutions, but I'm wondering if Apple is recommending this way, why isn't it working??


Solution

  • This can be done in a variety of ways. The simplest one is to try setting

    self.title = @"Main";
    

    This should be reflected in both your navigationBar title and tabBarItem title. Or , you could just connect them to the interface builder and type it out yourself and connect the outlets properly.