I have a:
@property(nonatomic, retain) NSArray * buttonsArray;
...
...
@synthesize buttonsArray;
when the view loads I initialize it as:
buttonsArray = [[NSArray alloc] initWithObjects:
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
nil];
// this code places the buttons from buttons array on top of the images in my view. I placed those images in an array called imagesArrayV;
int counter = 0;
counter=0;
for (UIButton *button in buttonsArray) {
button = [buttonsArray objectAtIndex:counter];
[button setTag:counter]; // *********
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Hello" forState:UIControlStateNormal];
UIImageView *tempImage = [imagesArrayV objectAtIndex:counter];
CGRect tempRect = tempImage.frame;
button.frame = tempRect;
[self.ViewMainV addSubview:button];
counter++;
}
the purpose of this is to save time creating all the buttons in xcode and creating the connections.

I posted a picture so that you can get an idea…
Anyways the method that get’s executed when clicking a button is:
-(void) test: (id) sender{
UIButton*btn = (UIButton*)(sender);
int tagnumber = [btn tag];
NSLog(@"%i",tagnumber);
}
why is it that when I press on the buttons the tag is equal to 0 when I set it to something else ( look for : // ********* ) when creating the button. Moreover when I run this method:
-(void) someOtherMethod{
int counter = 0;
for (UIButton *button in buttonsArray) {
button = [buttonsArray objectAtIndex:counter];
button.alpha = 0;
button.titleLabel.text = @"I change the title";
counter++;
}
}
the buttons that I previously added do not change at all. also the alpha does not change. I don’t know what button’s I am changing when I run the last method.
Just below the line where you set the tag, you have the line
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];. This overwrites the button obviously. The action is then added to the newly created button, the buttons in the array are left unaltered.Personally I would rewrite the code as follows:
This also avoids populating the array hardcoded, for more flexible, less error-prone code.