What is use of performSelector in iOS

QueueOverFlow picture QueueOverFlow · Jul 18, 2012 · Viewed 11.6k times · Source

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)];

Answer

James Webster picture James Webster · Jul 18, 2012

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]