Reputation: 19
What would be the code to modify the properties of a button (that was created programmatically) knowing its name and tag. (the amount of buttons created isn't always the same, that's why i assigned a tag to the buttons created)
Upvotes: 0
Views: 2242
Reputation: 2064
You could use viewWithTag:
UIButton *btn = (UIButton*)[self.view viewWithTag:1];
//then change the properties
[btn setTitle:@"Press Me" forState:UIControlState];
//etc etc
The advantage of this is that if you have multiple buttons with the same changes you can easily loop through the different buttons
for (int i=0; i<numberOfButtons; i++) {
UIButton *btn = (UIButton*)[self.view viewWithTag:i+1];
[btn setTitle:@"Press Me" forState:UIControlState];
//etc etc
}
This assumes your tags start from 1 and increment.
Upvotes: 1
Reputation: 12194
All you really need to identify a button is its tag. The code would look something like:
if (button.tag == 2) {
button.titleLabel.text = @"New text";
button.enabled = NO;
// etc...
}
Merely place that into whatever function you want to change your button(s) in.
Upvotes: 0