using tag attribute with a UIButton in the sender and trying to cast id -> UIButton

StackOverflow https://stackoverflow.com/questions/15166685

  •  16-03-2022
  •  | 
  •  

سؤال

First time using tag attributes and wondering what I'm doing wrong here. I have two UIButtons that go to the same selector. I would like to add a tag to differentiate like this:

buttonOne.tag=1;
buttonTwo.tag=2;

In the selector that responds, I'm trying to get this tag out of the sender but being told that tag is not found on object of type '__strong id'. I know this way is pretty hacky but is there a simple way to get this to work?

-(void)buttonClicked:(id)sender
{
  NSLog(@"you were clicked with %d", (UIButton *)sender.tag);
  [sender setSelected:YES];
}

thx in advance

هل كانت مفيدة؟

المحلول

Yap:

-(void)buttonClicked:(UIButton *)sender

Voila.

Or, if you're concerned to use that ugly cast, at least pay attention to operator precedence:

((UIButton *)sender).tag
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top