iosobjective-cuitabcontroller

Change the title in a UITabBarController


I've been trying to change the title of my tab with the following code,

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        self.title = @"City Search";
        self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
    }

    delegate = (AWSAppDelegate *)[[UIApplication sharedApplication] delegate];

    return self;
}

That gives me a iOS native search icon and test. but what I want to do is add my own title, and I tried doing the following,

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        self.tabBarItem = [[UITabBarItem alloc] init]
        self.tabBarItem.image = myImage;
        self.tabBarItem.title = @"FooBar";
    }

    delegate = (AWSAppDelegate *)[[UIApplication sharedApplication] delegate];

    return self;
}

However this just gives me a blank tab without any text, everything else works ok. can you please help? I'm using iOS 6


Solution

  • Following actually worked :) However I've noticed that if the image file does not exist, this won't work...

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        delegate = (AWSAppDelegate *)[[UIApplication sharedApplication] delegate];
    
        if (self) {
    
            self.title = @"City Search";
            [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"about_tap_icon.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"about_tap_icon.png"]];
            [self.tabBarItem setBadgeValue:@"about"];
            [self.tabBarItem setTitle:@"hello"];
        }
    
        return self;
    }