0

I have a method that shows 10 (or more) UIButtons. I have here a code as to how I showed these buttons..

-(void)showButtons{
for(int i = 0; i < 10; i++){
  UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
  button.frame = CGRectMake(x, y, 100, 94);  //Assume x and y have values
  **button.tag = i + 1000;**
  [button setBackgroundImage:[_cardImages objectAtIndex:i]  
      forState:UIControlStateNormal];
  [button addTarget:self action:@selector(myMethod:)   
      forControlEvents:UIControlEventTouchUpInside];
  [self.view add subview:button];
  [button release];

} }

-(IBAction)myMethod:(id)sender{
// I would like to print here button.tag, but I always get an error

}

4 Answers 4

3

You need to typecast the sender here because id types don't have the tag property.
The new code will be


-(IBAction)myMethod:(id)sender{
   UIButton *pressedButton = (UIButton *)sender;
   NSLog(@"Tag of button pressed:%d",pressedButton.tag);
}

5
  • Or you can say [sender tag], or you can change the declaration of the method to -(IBAction)myMethod:(UIButton *)sender.
    – rob mayoff
    Jan 30, 2012 at 3:53
  • tried this: -(IBAction)myMethod:(id)sender{ UIButton *pressedButton = (UIButton *)sender; NSLog(@"Tag of button pressed:%d",pressedButton.tag); } I got this error: [UITapGestureRecognizer tag]: unrecognized selector sent to instance 0x6f99140
    – JCurativo
    Jan 30, 2012 at 3:58
  • I think we would need to know more about the context to figure what's going on here. I believe that Kumar's answer is correct. Jan 30, 2012 at 4:44
  • Well then you have a UITapGestureRecognizer that you have connected to the myMethod. You need to edit your question and describe how the tap gesture recognizer is set up, and paste in any code that deals with the tap gesture recognizer.
    – rob mayoff
    Jan 30, 2012 at 4:47
  • @robmayoff - sorry, I'm editing my code now. I'll be using the addTarget:action method of the UIButton, instead of using the tapGesture. I just realized I was using the tapGesture in calling the myMethod function (which is why I can't print the tag). Thank you
    – JCurativo
    Jan 30, 2012 at 5:21
0

You will get the result by printing value of sender.tag instead of button.tag

-(IBAction)myMethod:(id)sender{
NSLog(@"%d",sender.tag);
}
2
  • i tried this inside the IBAction int tag = [sender tag]; NSLog(@"%d",tag);
    – JCurativo
    Jan 30, 2012 at 3:52
  • An id doesn't have a tag property. You have to cast sender to UIButton. I think Kumar's answer is correct. Jan 30, 2012 at 4:46
0

Try accessing the UIButton's UIView and then accessing it's TAG property. So modifying Amresh Kumar's code:

-(IBAction)myMethod {
 UIButton *pressedButton = (UIButton *)sender;
 NSLog(@"Tag of button pressed:%d",pressedButton.view.tag);
}
1
  • A UIButton doesn't HAVE a view; a UIButton IS a view. Kumar's answer is correct. Jan 30, 2012 at 4:43
0

Your myMethod's defination should be like this

I use the same way to get tag of sender.

-(IBAction)myMethod:(id)sender
{
   NSLog(@"%d",[sender tag]);
}

No type casting is needed, you may get tag of sender, it doesn't matter the datatype of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.