views:

35

answers:

1

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 button, but reference it by its title name?

+3  A: 

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.

James Huddleston