I want to change all the UIButton titles
My xib file has many buttons, similar to a phone key pad. I want to suddenly have all the keys to be randomly changed when a method like "randomizeAllButtons" is called. Of course setTitle: forState:UIControlSt开发者_JAVA百科ateNormal works in a UIButton method, but I can't get it done from a separate method. So much trouble trying to do this... any ideas.
Tag them (in Interface Builder, if you like). If you have 10 buttons, give them tag 0 to 9 or similar. That way you can get them via UIButton *btn=(UIButton *)[self.view viewWithTag:0];
and so on up to 9. Then it's just a matter of [btn setTitle:@"Button 0"];
<-- this is all you need to write to set the title.
You can add them to an NSMutableArray *btnArray
if you like, and jumble that - see docs.
You can do this in any method you like in the viewcontroller for the view with the buttons.
I think this is more generalized way to achieve your goal :
for (UIView *view in self.view.subviews)
{
if ([view isKindOfClass:[UIButton class]])
{
// change the title here ..
}
}
精彩评论