Why does not work changing UIButton text via dot syntax?

亡梦爱人 提交于 2019-12-12 21:17:59

问题


Is it possible to change UIButton title label text via dot syntax? The text is not changing if I do it via dot syntax:

self.myButton.titleLabel.text = [NSString stringWithString:@"Nop"];

But this one does:

[self.myButton setTitle: @"Yep" forState: UIControlStateNormal];

Text change works for pure UILabels, is UIButton a special case or something?


回答1:


This is because the text can be changed for a UIButton for several different states (Normal, Highlighted, etc). Apple simply chose to require explicit code to set the text. You could probably create a category to implement this, if you really wanted.




回答2:


UIButton's can reconfigure their titles for any of the UIControlState's (see here: UIControlState).

Since using the dot syntax doesn't set the title for any specific state, the UIButton is likely re-configuring the title for UIControlStateNormal, which is set to nothing.




回答3:


Try

myButton.titleLabel.text=@"foo";

here works

[]'s



来源:https://stackoverflow.com/questions/7000796/why-does-not-work-changing-uibutton-text-via-dot-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!