26

I have a Controller/View for a generic list of items, that can be extended for displaying a custom list.. Listing and navigation works fine.. but I can't change the title of UINavigationController. In the generic Controller:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview: navigationController.view];
}
- (void)setNavigationTitle: (NSString *)title
{
    NSLog(@"set title: %@", title); // this works
    self.navigationController.title = title; // Nothing works here
}

Then, the extended class does..

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNavigationTitle: @"Custom list"];
}

The navigationBar still have "Item" as title :(

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
grilix
  • 5,211
  • 5
  • 33
  • 34

5 Answers5

63

In your UIViewController there is a title property and it is that property that will be displayed by the NavigationController. So when pushing a new UIViewController onto the navigation stack set the title of that UIViewController to whatever is appropriate.

In your case it looks like it would be:

[self setTitle:@"WhateverTitle"];
willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
  • the first view is not pushed: [self.view addSubview: navigationController.view]; i tried also: self.title = @"AAAA"; [self.view addSubview: navigationController.view]; and does not works – grilix Oct 18 '10 at 12:27
  • In the pushed views (when user selects an item), the title is changed correctly – grilix Oct 18 '10 at 12:36
  • I notice that you didn't set a rootViewController for navigation controller. The first view controller in navigationController is called rootViewController. You may set its title using self.title = @"xxx". – Chilly Zhong Oct 18 '10 at 15:03
  • my navigationbar does not have a "rootViewController" :? – grilix Oct 18 '10 at 15:14
  • self.title = @"Title"; is working – Tom Jan 19 '22 at 01:41
14

For those looking for a Swift solution:

class CustomViewController: SuperViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "My Custom Title"
  }
}

The title needs to be set on the UIViewController and not on the UINavigationControllerembedding the view controller.

Documentation: UIViewController Class Reference: title Property

fiedl
  • 5,667
  • 4
  • 44
  • 57
5

use

self.title = @"yourTitle";
Gyani
  • 2,241
  • 1
  • 24
  • 38
  • I have now: NSLog(@"set title: %@", title); self.title = title; self.navigationItem.title = title; self.navigationController.title = title; // none of these works :/ – grilix Oct 18 '10 at 12:34
  • set title in - (void)viewWillAppear:(BOOL)animated method – Gyani Oct 18 '10 at 12:37
4

Swift

title = "whateverTitle". It's a UIViewController instance property ; invoke anytime.

From the documentation:

Declaration
var title: String? { get set }

Discussion
Set the title to a human-readable string that describes the view. If the view controller has a valid navigation item or tab-bar item, assigning a value to this property updates the title text of those objects.

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
0

I know its old thread but thought of sharing this Set the 'self.title' in 'init' method in UIVIewControler derived class, This worked for me!!

infiniteLoop
  • 2,135
  • 1
  • 25
  • 29