Why can't I change the name of UIBarButtonItem?

I want to change the UIBarButtonItem title .

But this code does not work.

 - (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 do not know how to solve this problem.

Please tell me a way to solve this problem.

+6
source share
4 answers

item.title = @"new title" should work.

-3
source

Initialize your UIBarButtonItem as follows:

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

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

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

IOS 8 and above

UIBarButtonItemStyleBordered is deprecated, so use below.

 [[UIBarButtonItem alloc] initWithTitle:@"Your Title" style:UIBarButtonItemStylePlain target:self action:@selector(yourMethod)]; 
+5
source

If you want to change the title of the navigation bar programmatically, then

 UIBarButtonItem *aDoneItem = [UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEdit:)]; navigationController.rightBarButtonItem = aDoneItem; aDoneItem.title = @"Something"; 
0
source

You must make sure that the button is initialized. If it is part of a .xib file, make sure it is associated with your @property , which is also associated with IBOutlet . Otherwise, you need to initialize it programmatically. In addition, you should use the setTitle:forState: method.

-1
source

Source: https://habr.com/ru/post/952215/


All Articles