How can I set the Title for a UINavigation Bar programmatically?
Thanks in advance.
How can I set the Title for a UINavigation Bar programmatically?
Thanks in advance.
self.title = @"My View";
or
navigationBar.topItem.title = @"My View";
Depending on if you are using a UINavigationController
or not.
in your view controller implementation file:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.title = @"My Title";
}
return self;
}
this should work.
The title that is shown in UINavigationBar comes from the currently active UIViewController.
For example, let's say we want a UINavigationController with a root view controller called MyCustomViewController.
In our app delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIViewController *myCustomViewController = [[MyCustomViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:myCustomViewController];
[window addSubview:navController.view];
[window makeKeyAndVisible];
}
In MyCustomViewController.m:
- (void)viewDidLoad {
self.title = @"Hello World!";
}