简体   繁体   中英

Updating UINavigationBar Title

I have a split view controller and a navigationcontroller on the left side. As views are pushed onto the nav controller I'm attempting to change the Navigation Bar title. The problem seems to be that the text can only be updated in viewWillAppear . If I try to change the text after that initial load, it doesn't update. At first I thought I needed to call setNeedsDisplay on the NavigationItem, but it doesn't have a property. I don't understand because the same code works in viewWillAppear but not once the view is shown.

I'm using:

(void)viewWillAppear:(BOOL)animated{
        self.title = @"blah";
        ...
}

它应该是self.navigationItem.title = @"Some Title";

I've set up a simple project where initial controller is embedded in navigation controller, and the second controller is pushed onto the navigation stack. Second controller has a button which calls this bit of code:

- (IBAction)changeTitle:(id)sender {

    static unsigned int i = 1;

    self.navigationItem.title = [NSString stringWithFormat:@"Number of changes: %ud", i];

    i++;

}

It works as intended. Clearly it happens after all appear/layout events. So your problem lies somewhere else. Maybe self that you are referring to is a wrong controller.

self.navigationBar.topItem.title = [NSString stringWithFormat:@“ TOP”];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM