I am reusing some code that build UIButtons dynamically (without interface builder). I am using the addTarget:action:forControlEvents method to perform an action each time a button is pressed. I change the background color of the button based on the setSelected and isSelected properties.
I have added a reset button in order to deselect all buttons at once. However, I am having difficulty determining how to access the properties of a dynamically added button.
THe code to create the buttons is as follow:
for(int y = 1; y < 10; y++)
{
for(int x = 1; x < 5; x++){
z++;
aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(x*x_plot, y_plot, 60, 40);
[aButton setBackgroundImage:[UIImage imageNamed:@"btnUnselected.png"] forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(digitClick:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:[NSString stringWithFormat:@"%d",z] forState:UIControlStateNormal];
aButton.titleLabel.textColor = [UIColor blackColor];
aButton.tag = z;
[self.view addSubview:aButton];
}
y_plot=y_plot+45; //make a 4x9 grid of buttons
}
You could use code like this to go through all your subviews