Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TomCuthill

macrumors newbie
Original poster
Nov 19, 2010
3
0
I have created buttons, each with their own tag.

Code:
int tag;
	for (int i = 0; i < N_COLS; i++) {
		for (int j = 0; j < N_ROWS; j++) {
			soundButton = [UIButton buttonWithType:UIButtonTypeCustom];
			soundButton.frame = CGRectMake(i * 20.0, j * 20.0, 20.0, 20.0);
			tag=j + i * N_ROWS + 1;
			soundButton.tag = tag;
			buttonStatus[tag] = NO;
			[soundButton setImage:[UIImage imageNamed:@"BWhite2.png"]
					 forState:UIControlStateNormal];
			[soundButton addTarget:self action:@selector(buttonPushed:)
			  forControlEvents:UIControlEventTouchUpInside];
			[self.view addSubview:soundButton];			
		}
}

These tags have already been used in the buttonPushed: method
Code:
- (void) buttonPushed:(id)sender {
	UIButton *soundbutton = sender;
	NSLog(@"buttonPushed tag=%d", soundbutton.tag);
	
	int tag = soundbutton.tag;
	
	buttonStatus[tag] = !buttonStatus[tag];
	
	NSLog(@"Status=%d", buttonStatus[tag]);
	
	if (buttonStatus[tag]==YES) {
		[soundbutton setImage:[UIImage imageNamed:@"BBlue.png"]forState:UIControlStateNormal];
	}
	else {
		[soundbutton setImage:[UIImage imageNamed:@"BWhite2.png"]forState:UIControlStateNormal];
	}

}

How do I use the tags in another method? I have searched elsewhere but could not find the answer.

Many thanks,
Tom.
 
You can search for a UIView having the defined tag:

Code:
for (UIView* v in [self.view subviews]) {
    if ([v tag] == ...) {
        // ...
    }
}

Hope that helps :)
 
How do I use the tags in another method?
This is an awfully vague question. Could you please elaborate more? What does this other method do? Where does it reside? (i.e. in the same view controller?) What do you mean by "use"? That is, what do you intend to do with the buttons / tags?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.