What is the role of performSelector?
Comparing:
[self btnClicked];
and
[self performSelector:@selector(btnClicked)];
-(void)btnClicked
{
NSLog(@"Method Called");
}
both are woking fine for me. What is difference between these two. [self btnClicked]
and [self performSelector:@selector(btnClicked)]
;
The two are pretty identical when used as you have demonstrated, but the latter has the advantage that you can dynamically determine which selector to call at runtime.
SEL selector = [self gimmeASelectorToCall];
[self performSelector: selector];
[Source]
Apple doc is your friend.
NSObject Protocol Reference
It
Sends a specified message to the receiver and returns the result of the message.
In particular:
The
performSelector:
method is equivalent to sending an aSelector message directly to the receiver. For example, all three of the following messages do the same thing:id myClone = [anObject copy]; id myClone = [anObject performSelector:@selector(copy)]; id myClone = [anObject performSelector:sel_getUid("copy")];
However, the performSelector: method allows you to send messages that aren’t determined until runtime. A variable selector can be passed as the argument:
SEL myMethod = findTheAppropriateSelectorForTheCurrentSituation(); [anObject performSelector:myMethod];
The aSelector argument should identify a method that takes no arguments. For methods that return anything other than an object, use NSInvocation.
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With