Sunday, June 9, 2013

UINavigationItem setTitle example in Objective C (iOS).

UINavigationItem setTitle

The navigation item’s title displayed in the center of the navigation bar.

@property(nonatomic, copy) NSString *title

Discussion of [UINavigationItem setTitle]
The default value is nil.

When the receiver is on the navigation item stack and is second from the top—in other words, its view controller manages the views that the user would navigate back to—the value in this property is used for the back button on the top-most navigation bar. If the value of this property is nil, the system uses the string “Back” as the text of the back button.

UINavigationItem setTitle example.
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.

Example of [UINavigationItem setTitle].
CGRect navBarFrame = CGRectMake(0, 0, self.tableView.frame.size.width, 44.0);
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:navBarFrame];

UINavigationItem *navItem = [UINavigationItem alloc];
navItem.title = @"Your Title";

[navBar pushNavigationItem:navItem animated:false];

UINavigationItem setTitle example.
@synthesize navBar;
-(void)viewWillAppear:(BOOL)animated {
[self.navBar setTitle:@"Sign In"];
[self.navigationController setNavigationBarHidden:NO animated:animated];

End of UINavigationItem setTitle example article.