tags:

views:

297

answers:

3

How can I set the Title for a UINavigation Bar programmatically?

Thanks in advance.

A: 
self.title = @"My View";

or

navigationBar.topItem.title = @"My View";

Depending on if you are using a UINavigationController or not.

marcc
Since I was not using a UINavigationController I used the second syntax after creating an IBOutlet to navigationBar. Thank you.
jp chance
A: 

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.

Shivan Raptor
A: 

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!";
}
richleland