Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change navbar title programmatically

I'm working with the Xcode Utility template. In rootViewController.m there's a section that sets up the navbar for what's called the FlipSideView:

(snip)

UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Title"];

(snip)

How can you programmatically change the navbar title from within the FlipsideViewController class? I tried:

self.navigationItem.title = @"XXXXXX";

w/o success.

like image 247
Alan Avatar asked Dec 17 '22 08:12

Alan


1 Answers

OK, I spent some time on this, because I want to use the convenience of NIB layout for the utility-template app, but I want to be able to internationalize without maintaining a gazillion nibs.

So, the first thing is to add to the FlipViewController class:

IBOutlet UINavigationBar *navBarItself;

Then, in IB, to control-drag from the Navigation Bar to the File's Owner, and connect to navBarItself in the File's Owner.

Then, the code:

- (void) viewWillAppear:(BOOL) animated {

    [super viewWillAppear:animated];

    navBarItself.topItem.title = @"Peace!!";
}

and this appears to work like a charm

(and it also works when added to ViewDidLoad)

(p.s. the other answers shown here did not work for me)

like image 134
George D Girton Avatar answered Dec 24 '22 00:12

George D Girton