Objective-C Tips & Trick - Prioregroup.com
[Objective-C] How to invoke a method in a class via their string names
       
back

+ (void)invokeMethodNamed:(NSString*)methodName withObject:(id)object classNamed:(NSString*)className
{
                    id class = NSClassFromString(className);
                    if (class) {
                        SEL selector = NSSelectorFromString(methodName);
                        if ([class respondsToSelector:selector]) {
                            // static method +(void)
                            [class performSelector:selector withObject:object];
                        } else {
                            // new instanced method -(void)
                            id object = [[[class alloc] init] autorelease];
                            if ([object respondsToSelector:selector]) {
                                [object performSelector:selector withObject:object];
                            }
                        }
                    }
}






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com