为什么我不能改变UIBarButtonItem的标题?

Ruk*_*ora 6 iphone objective-c uibarbuttonitem ios

我想改变UIBarButtonItemtitle.

但是这段代码不起作用.

- (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]
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决这个问题.

请告诉我解决这个问题的方法.

hgw*_*tle 5

像这样初始化你的UIBarButtonItem:

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

self.navigationItem.rightBarButtonItem = _smay;
Run Code Online (Sandbox Code Playgroud)

然后,如果要在代码中的其他位置更改标题:

[_smay setTitle:@"Your Other Title"];
Run Code Online (Sandbox Code Playgroud)

IOS 8及更高版本

不推荐使用UIBarButtonItemStyleBordered,因此请使用以下命令.

[[UIBarButtonItem alloc] initWithTitle:@"Your Title" 
                 style:UIBarButtonItemStylePlain
                 target:self action:@selector(yourMethod)];
Run Code Online (Sandbox Code Playgroud)


sup*_*org -3

item.title = @"new title"应该管用。