Search code examples
iosuinavigationcontrolleruinavigationbaruinavigationitemuistoryboard

Can't set UINavigationItem title programmatically


I set up my app using a storyboard and have my main view controller embedded in a UINavigationControler. To change the title that appears in the navigation bar, in the viewDidLoad method of my main view controller, I have self.navigationItem.title = @"My Title"; But the title never gets set.

My guess is that I need to set up a Reference Outlet in my storyboard, but I'm not sure what needs to be connected to what. Any ideas?


Solution

  • in story board, it doesn't get automatically connected , make a UINavigationItem using the following code in ur .h file

    @property(weak, nonatomic) IBOutlet UINavigationItem *navBar;
    

    in .m file synthesize the property and set the title like this

    @synthesize navBar;
    -(void)viewWillAppear:(BOOL)animated {
    [self.navBar setTitle:@"Sign In"];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    }
    

    Also, don't forget to connect the UINavigationItem "navBar" in the storyBoard with ur class so that storyboard knows whose title to change. In case u don't have a UINavigationItem in ur storyboard,add it outside the UIView and then connect it properly