开发者

Target a UIButton by it's title

I am setting the title of a UIButton dynamically using [btn setTitle:x forState:UIControlStateNormal]. Is it possible to say for instance, change the background image of that but开发者_运维技巧ton, but reference it by its title name?


Using button titles like that is a bad idea. If your application gets localized for a different language, the button is likely to end up with a different title and your code will break.

However, UIView has something called a tag. A tag is just an integer value that you can assign to a view for identification purposes. If you assign your buttons unique tag values, you can find a specific button by asking its superview to search for it. You do this by sending the viewWithTag: message to the button's superview:

NSInteger kTagForMyButton = 7;

UIButton *myButton = (UIButton *)[theSuperview viewWithTag:kTagForMyButton];

Unless you have a good reason to use tags, I'd recommend maintaining a reference to the button so you don't have to search for it every time you want to change it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜