Question

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!!

Was it helpful?

Solution

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).

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top