rahul
rahul

Reputation: 147

How to use Tag on dynamic button in IOS?

I have created buttons by using for loop. The buttons are made perfectly fine. Now I have to single IB action for all of the dynamic buttons. I have set tag too.The code I am using is below written. But the action is not identifying the exact button which I want to use.

for (int i=0; i < 10; i++){

     btnphoto=[[UIButton alloc]initWithFrame:CGRectMake(10,(30*i)+110,50,20)];

     [btnphoto setTitle:@"Photo" forState:UIControlStateNormal];

     [btnphoto addTarget:self action:@selector(someMethod:)forControlEvents:UIControlEventTouchUpInside];

     btnphoto.tag=100 + i;

     [self.view addSubview:btnphoto];

}

-(void)someMethod:(UIButton *)sender{

}

Upvotes: 1

Views: 10445

Answers (7)

Om Prakash
Om Prakash

Reputation: 9461

Try the above code, this might be helpful :

-(void)someMethod:(id)sender{
   UIButton *btn = (UIButton *)[self.view viewWithTag:btn_tag];
    switch([btn tag])
    {
     case :101
       [btn setBackgroundColor:[UIColor redColor]];
        break;

      case :102
           [btn setBackgroundColor:[UIColor redColor]];
           break;

      case :103
           [btn setBackgroundColor:[UIColor blueColor]];
           break;
   }
}

Upvotes: 0

Tapas Pal
Tapas Pal

Reputation: 7207

As you have already set the tag for each button so just do the below to identify which button has been touched

-(void)someMethod:(id)sender
{
    UIButton *TouchedButton=(UIButton *)sender;
    int TouchedButtonTag=[TouchedButton tag]; // Now you can uniquely identify your buttons by this tag. 
    // do your stuff for respective button touched event

   switch([TouchedButton tag])
   {
      case :101
           [TouchedButton setBackgroundColor:[UIColor redColor]];
           break;

      case :102
           [TouchedButton setBackgroundColor:[UIColor redColor]];
           break;

      case :103
           [TouchedButton setBackgroundColor:[UIColor blueColor]];
           break;

     // and so on...
   }
}

Upvotes: 0

Rajneesh071
Rajneesh071

Reputation: 31083

Try this

for (int i=0; i < 10; i++){

    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(10,(30*i)+110,50,20)];

    [button setTitle:@"Photo" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(someMethod:)forControlEvents:UIControlEventTouchUpInside];

    button.tag=100 + i;

    [self.view addSubview:button];

}

To show diffrent color, first add color in array

NSMutableArray *colorArray=[[NSMutableArray alloc] initWithObjects:[UIColor redColor],[UIColor blueColor],[UIColor greenColor],nil];    

then

-(void)someMethod:(UIButton *)sender{
    NSLog(@"Btn Tag = %d",sender.tag);
    int randomNum=arc4random()%3;
    [sender setBackgroundColor:[colorArray objectAtIndex:randomNum]];
}

Upvotes: 3

Mick MacCallum
Mick MacCallum

Reputation: 130193

You have to actually access the tag property of the button from within the function to determine which button has called the selector.

-(void)someMethod:(UIButton *)sender {

    UIButton *tappedButton = sender;

    NSInteger tag = tappedButton.tag;
    NSLog(@"This is the button with tag: %ld",tag);

    [tappedButton setBackgroundColor:[UIColor redColor]];
}

Upvotes: 2

BhavikKama
BhavikKama

Reputation: 8790

if you want to change the color or any property then you can do as follows

//i just giving you example of how can i change the title of the UIButton same way you can change color property... i have just used this in my some code..

 -(void)someMethod:(id)sender {
          //1...this will change all the button title with string "Changed" 
            [sender setTitle:@"Changed" forState:UIControlStateNormal];


         //2..now if you want to just change the property of uibutton on basis of tag value
             [(UIButton *)[self.contentView viewWithTag:Your_TAG_VALUE] setTitle:@"JustChanged" forState:UIControlStateNormal];


    }

Upvotes: 2

Om Prakash
Om Prakash

Reputation: 9461

-(void)someMethod:(id)sender{     

       UIButton *btn = (UIButton *)sender;
       int *btn_tag=btn.tag;

       NSLog(@"This is the button with tag: %d", btn_tag);


    }

Upvotes: 0

iSmita
iSmita

Reputation: 1292

Try this -

In someMethod method -

UIButton *button = sender;

int buttonTagIndex = button.tag;

Now buttonTagIndex gives you tag index for selected button.

Upvotes: 3

Related Questions