Created
August 17, 2012 23:17
-
-
Save rugginoso/3383407 to your computer and use it in GitHub Desktop.
Dynamically call delegate method by name in Objective-C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)dispathCommand:(NSString *)command withParameters:(NSArray *)parameters | |
{ | |
SEL *selector = NSSelectorFromString([NSString stringWithFormat:[@"handle" stringByAppendingString:[command capitalizedString]]); | |
if (![self.delegate repondsToSelector:selector]) { | |
[self handleUnknownCommand:command withParameters:parameters]; | |
return; | |
} | |
NSMethodSignature *signature = [[self.delegate class] instanceMethodSignatureForSelector:selector]; | |
if (signature != nil) { | |
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; | |
[invocation setTarget:self.delegate]; | |
[invocation setSelector:selector]; | |
[invocation setArgument:¶meters atIndex:2]; | |
[invocation invoke]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment