Setting title in setItem: vs in viewWillAppear: vs in init:?


Hello All,

Apologies in advance if this is a trivial/nonsense question. As far I understand, UINavigationItem’s title property can be set in various different ways:

In Chapter 10, BNRItemsView’s title is set in the instance method:

instance method from BNRItemsViewController.m

- (instancetype) init
    // Call the superclasses designated initalizer
    self = [super initWithStyle:UITableViewStylePlain];
     if (self){
         // *****Here we set the title*****
         UINavigationItem *navItem = self.navigationItem;
         navItem.title = @"Homepwner";
         //Create a new bar button item that will send addNew: to BNRItemsViewController
         UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem:)];
         //Set this bar button item as the right item in the navigationItem
         navItem.rightBarButtonItem  = bbi;
         navItem.leftBarButtonItem = self.editButtonItem;
    return self;

For the BNRDetailView, we set it in the setItem: method by overriding the method:

part of BNRDetailViewController.m


  • (void) setItem:(BNRItem *)item
    _item = item;
    self.navigationItem.title = _item.itemName;

Also, I tried to set it in the viewWillAppear method, and it works from there too:

I put this in viewWillAppear: method in BNRDetailViewController.m

UINavigationItem *navItem = self.navigationItem;
 navItem.title = item.itemName;

So my question is basically which one is the suggested usage and are there any differences for setting the title in the init: method, viewWillAppear: method and the setItem: method…

Any feedback is much appreciated!

thanks a lot,


In BNRItemsViewController, the nav item’s title is constant, so it need be set only once for the life of the controller object, which makes -init a great choice.

In BNRDetailViewController, the nav item’s title is NOT constant – it’s set to a field that’s part of an object that’s passed in, so an “obvious” place to do that is when storing a reference to that passed-in object, which occurs in -setItem:.

But, as you saw on page 204, other fields are pulled from the BNRItem in -viewWillAppear, so to me (I can’t speak for BNR), that seems like a reasonable place to set the nav title as well.

As for which is “better”…I’d do as the book suggests. It all boils down to this: where would you EXPECT to find it six months from now when you come back to this code?


Thank you very much for taking the time to answer this gc3182, much appreciated.

All the use cases makes sense now, and “6 months rule” is a good suggestion. Thanks again.