Sunday, June 9, 2013

UINavigationBar topItem example in Objective C (iOS).


UINavigationBar topItem

The navigation item at the top of the navigation bar’s stack. (read-only)

@property(nonatomic, readonly, retain) UINavigationItem *topItem

UINavigationBar topItem example.
OK, I assume you have a reference to your UINavigationBar? Then I guess you'd add a single UINavigationItem to it:

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"A Title"];
theNavigationBar.items = [NSArray arrayWithObject:item];
[item release]; // or keep this as an instance variable
and then set that item's left and right buttons:

theNavigationBar.topItem.leftBarButtonItem = ...;
theNavigationBar.topItem.rightBarButtonItem = ...;

Example of [UINavigationBar topItem].
I've set the title programatically using code something like this:

navBar.topItem.title = @"title";
where navBar is declared as an IBOutlet UINavigationBar linked to the navigation bar in interface builder. This worked in my app; however, I was not using a tab bar.

If navBar.topItem is the tab bar item, I don't see a way for you to change the title that appears on the navigation bar without also changing the title on the tab bar item, since the navBar's topItem and the tab bar item is the same object.

UINavigationBar topItem example.
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"title text"];
...
[bar pushNavigationItem:item animated:YES];
[item release];
or

bar.topItem.title = @"title text";

End of UINavigationBar topItem example article.