Button Tag Problem

Discussion in 'iOS Programming' started by TomCuthill, Dec 13, 2010.

  1. TomCuthill macrumors newbie

    Joined:
    Nov 19, 2010
    #1
    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.
     
  2. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #2
    You can search for a UIView having the defined tag:

    Code:
    for (UIView* v in [self.view subviews]) {
        if ([v tag] == ...) {
            // ...
        }
    }
    Hope that helps :)
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    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?
     

Share This Page