Reputation: 3027
I have 3 buttons which call the same function. I want to know inside the function which button called it. Is it possible to know it?
Upvotes: 1
Views: 322
Reputation: 95335
Yes, you can use the sender
argument. If you have an IBOutlet called buttonOne
, you can check inside the IBAction method like this:
- (IBAction) buttonClicked:(id) sender
{
if (sender == buttonOne)
{
NSLog(@"Button one was pressed.");
}
}
Alternatively, assign each of your buttons a tag
, and use the sender's tag
property (the following example assumes that buttonOne
was assigned the tag value “1”):
- (IBAction) buttonClicked:(id) sender
{
if ([sender tag] == 1)
{
NSLog(@"Button one was pressed.");
}
}
Upvotes: 6