Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I change UIBarButtonItem's title?

I want to change UIBarButtonItem's title.

But this code is not working.

- (void)viewDidLoad {
    ...        
    [self smay];
}

- (void)smay {
    ...

    AppDelegate *apd = (AppDelegate*)[[UIApplication sharedApplication]delegate];
    NSString *SmayUse = [NSString stringWithFormat:@"%d月%d日",
                           apd.newyear,apd.newmonth];
    [_smay setTitle:SmayUse]
}

I don't know how to solve this problem.

Please tell me a way to solve this problem.

like image 710
Rukkora Avatar asked Aug 21 '13 14:08

Rukkora


1 Answers

Initialize your UIBarButtonItem like this:

_smay = [[UIBarButtonItem alloc] initWithTitle:@"Your Title" 
             style:UIBarButtonItemStyleBordered 
             target:self action:@selector(yourMethod)];

self.navigationItem.rightBarButtonItem = _smay;

Then, if you want to change the title somewhere else in your code:

[_smay setTitle:@"Your Other Title"];

IOS 8 and higher

UIBarButtonItemStyleBordered is deprecated, so use the below.

[[UIBarButtonItem alloc] initWithTitle:@"Your Title" 
                 style:UIBarButtonItemStylePlain
                 target:self action:@selector(yourMethod)];
like image 69
hgwhittle Avatar answered Sep 28 '22 12:09

hgwhittle