views:

2212

answers:

2

I have a UITabBarController that manages 5 View Controllers. I create their tab bar items in their "init" methods so that they will be displayed before the view is loaded. I'm just wondering what way I should do it, because there seems to be so many ways. For example, for my DatePickerViewController:

- (id)init {
    if((self = [super init])) {
     // ================ THIS ==========================
     UIImage *clockIcon = [UIImage imageNamed:@"clockicon.png"];
     UITabBarItem *localTabBarItem = [[UITabBarItem alloc]
          initWithTitle:@"Date" image:clockIcon tag:0];
     [self setTabBarItem:localTabBarItem];
     [localTabBarItem release];
     // ================ OR THIS ========================
     [self setTitle:@"Date"];
     UITabBarItem *localTabBarItem = [[UITabBarItem alloc] init];
     [localTabBarItem setImage:[UIImage imageNamed:@"clockicon.png"]];
     [self setTabBarItem:localTabBarItem];
     [localTabBarItem release];
     // ================ OR THIS ========================
     UITabBarItem *localTabBarItem = [[UITabBarItem alloc] init];
     [localTabBarItem setTitle:@"Date"];
     [localTabBarItem setImage:[UIImage imageNamed:@"clockicon.png"]];
     [self setTabBarItem:localTabBarItem];
     [localTabBarItem release];
    }
    return self;
}

Which way should I do it? And why is there a title for both the tabBarItem and the View Controller? And I don't think I need the tag (which is set in the first method).

Thanks!!

+1  A: 

Well in my opinion any of these ways are ok, it might be more readible when you declare the UIImage in one line and set it in a different line rather than doing it all inline, but at the end you get the same result.

The TabBarItems have a title which is the text that will show in the tab bar item iteself. View Controllers have a title for Navigation Controller purposes, the View Controllers title is displayed in the NavigationControllers NavBar when set. And you do need tags, tags is the way you tell the buttons apart when someone click on them (when u manage the TabBar on your own).

Daniel
1: I'm not managing the tab bar, UITabBarController is. 2:viewController.title property is not for navigation controllers, UIViewController's also have viewController.navigationItem.title property for the title.. So what is the view Controller's title property for?
Mk12
its for the NavigationController to know what title to display on the navbar when it pushes a view controller (one function anyway). Read it up here http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html where it says Updating the Navigation Bar
Daniel
It says, and i quote "If no custom title view is set, the navigation bar displays a label containing the view controller’s default title. The string for this label is usually obtained from the title property of the view controller itself. If you want to display a different title than the one associated with the view controller, set the title property of the view controller’s navigation item instead. "
Daniel
A: 

The reason there are several ways to set the title is for convienece. You may want to display one title in the navigation bar and one title in the tab bar.

This actually quite common since there is less space to display text in the tab bar.

Like many things in Cocoa, there is more than one way to do it. The only "correctness" you need to be concerned about is what works best for your situation.

Corey Floyd
But then what's the point of the *View Controller's* title? If the tab bar item and the navigation item already have their own title...
Mk12