selector snippets

How to implement callbacks in Objective-C by passing methods as parameters

Tagged method, callback, selector  Languages objectivec

Example of a method that takes a callback method, defined as theSelector, as a parameter:

- (void) getURL:(NSString *)theURL theSelector:(SEL)theSelector
    NSURL *url = [NSURL URLWithString:theURL];
    ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
    [request setDelegate:self];
    [request setDidFinishSelector:theSelector];
    [request setDidFailSelector:@selector(onError:)];
       [networkQueue addOperation:request];
    [networkQueue go];
// Don't forget the semicolon
SEL sel = @selector(onResponseReceived:);
[self getURL:@"" selector:sel];
- (void)onResponseReceived:(ASIHTTPRequest *)request {