为什么我无法更改 UIBarButtonItem 的标题?

2024-05-23

我想改变UIBarButtonItem's title.

但这段代码不起作用。

- (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]
}

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

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


像这样初始化你的 UIBarButtonItem:

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

self.navigationItem.rightBarButtonItem = _smay;

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

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

iOS 8 及更高版本

UIBarButtonItemStyleBordered 已弃用,因此请使用以下内容。

[[UIBarButtonItem alloc] initWithTitle:@"Your Title" 
                 style:UIBarButtonItemStylePlain
                 target:self action:@selector(yourMethod)];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我无法更改 UIBarButtonItem 的标题? 的相关文章

随机推荐