Sahil Chaudhary
Sahil Chaudhary

Reputation: 503

calling button action function

just a quick question...say I have created a button and added some action to it:

-(IBAction)btnclicked:(id)sender {
        //some code here
}

And now I wanna call it so i can do this:

[self btnclicked:self];

So my question is what role does the self after the btnclicked play? why will it be wrong to write it this way:

[self btnclicked:sender];

please help.

Upvotes: 1

Views: 755

Answers (1)

wattson12
wattson12

Reputation: 11174

sender is type id, which can be any objective-c object (or nil). So you can pass whatever you like into the method and the compiler will be happy.

what you do inside that method may be excpeting a UIControl of some sort though, so if you pass in a UIView or NSString or anything else, there could be an unrecognized selector or some other crash. In other words, you can pass in what you like, as long as you know what you're doing.

for your question about it being wrong to pass in sender. I am guessing that is giving a compiler warning because sender is undefined in the context you are using it. sender is the variable name given to the argument inside the method, it is not a global variable or constant that can be passed around

Upvotes: 1

Related Questions