Sat
Sat

Reputation: 1626

What is passed as Sender on IBAction?

When I use the code -(IBAction) onClick1: (id) sender;, what will be passed as sender? I tried to use the sender as the object id and fails (I mean I used the code sender.tag and it didn't return).

I am sure that when I am using - (void)buttonTouched1:(UIButton *)sender; here the sender should act as an object id.

Upvotes: 3

Views: 6717

Answers (2)

Chuck
Chuck

Reputation: 237110

The sender variable is normally the object that sent the action message (this isn't exactly guaranteed — for example, you can send an action message yourself and pass anything you want — but that's how it's supposed to work).

You can't just write sender.tag in those methods because dot notation requires the object to have a static type so the compiler knows what message the property corresponds to. If you write [sender tag] instead, it will work.

Upvotes: 4

galexyus
galexyus

Reputation: 131

The sender is usually the object that will invoke the onClick1 message. For example if you have a button and you associate your -(IBAction)onClick1:(id)sender message with the "touch up inside" event of the button, then when you tap on the button, the object representing the button will be passed to onClick1:

-(IBAction)onClick1:(id)sender
{
   UIButton *button = (UIButton*)sender;
   NSLog(@"%@", button);
}

Upvotes: 6

Related Questions