不可能更改UIBarButtonItem标题? [英] Changing UIBarButtonItem title impossible?
问题描述
愚蠢的是,如果满足for循环中的某个条件,我正在尝试更改UIBarButtonItem上的标题。但是我遇到了麻烦。
Silly thing, I am trying to change the title on a UIBarButtonItem if a certain condition in a for-loop is met. However I'm having trouble.
我试过了
self.barButtonItem.title=@"NewTitle"
和
[self.barButtonItem setTitle:@"New Title"];
我甚至在视图上做了一个setNeedsDisplay而没有任何运气。顺便说一句,是的,我从self.navigationBar.items获取旧的按钮数组,删除旧按钮,并设置新按钮。
I even did a setNeedsDisplay on the view afterwards without any luck. By the way, yes I am getting the old array of buttons from self.navigationBar.items, removing the old button, and setting the new one.
如果我改变了屏幕方向(翻转ipad),按钮标题确实改变。但除此之外,它保持不变。
If I change the screen orientation (flip ipad), the button title does change. But otherwise, it stays the same.
我错过了什么?
推荐答案
问题
如果您浏览文档,Apple会说:
If u explore the documentation, apple says :
你应该在将项目添加到栏之前设置此属性。
"You should set this property before adding the item to a bar."
因此,
cancel_btn.title = "something" // It does not work !
因为您已将项目设置为栏。
Because u already set the item to the bar.
解决方案
//
let theTitle = "something"
//
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: theTitle, style:
UIBarButtonItemStyle.plain, target: self, action:
#selector(cancelAction))
现在你在将项目添加到栏之前设置标题。
Now u set the title before adding the item to the bar.
这篇关于不可能更改UIBarButtonItem标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!