Search code examples
iosobjective-cframenulluinavigationitem

navigationItem always become nil


I have IBoutlet for navigationItem and I write as

@property (nonatomic,strong) IBOutlet UINavigationItem *navItemRoster;

Then, I write to set title view.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
        self.title = @"Select User";
        self.navItemRoster.titleView = [Helpers getCenterTitleViewWithTitle:@"Select User"];
    }
    return self;
}

But I can't set title view. So, I check and debug. I found out this. It is always nil. How shall I do? What are the possible mistakes?

enter image description here

Edited: I have also connected in IB.

enter image description here


Solution

  • If you are not opening this VC using this method - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil then you have to write this code in -(void)viewDidLoad method to set the title.

    -(void)viewDidLoad {
        [super viewDidLoad];
        self.title = @"Select User";
        self.navItemRoster.titleView = [Helpers getCenterTitleViewWithTitle:@"Select User"];
    }