有没有办法在iPhone的[self performSelector]
方法中传递参数?以及在此方法中传递最大参数的数量是多少?如果是,那么请发送任何链接或任何想法来开发此功能。
答案 0 :(得分:7)
如果您想传递不超过两个参数,请使用
之一- (id)performSelector:(SEL)aSelector
- (id)performSelector:(SEL)aSelector withObject:(id)anObject
- (id)performSelector:(SEL)aSelector withObject:(id)anObject withObject:(id)anotherObject
如果您需要传递更多参数,请使用NSInvocation
。以下是docs。
UPD:这是一个NSInvocation
示例。比如,您要将doThis:andThis:andThis:
发送到targetObject
类型的MyClass
:
SEL message = @selector(doThis:andThis:andThis:);
NSMethodSignature *signature = [MyClass methodSignatureForSelector:message];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:targetObject];
[invocation setSelector:message];
[invocation setArgument:&fist atIndex:2]; // Note that you need to put &
[invocation setArgument:&second atIndex:3]; // as you send a pointer
[invocation setArgument:&third atIndex:4]; // Also the indexing starts from 2
// 0 is for target, 1 is for selector
[invocation invoke];
答案 1 :(得分:2)
使用
- (id)performSelector:(SEL)aSelector withObject:(id)anObject
侧击
答案 2 :(得分:1)
总体而言,您有三种可能性:
1. – performSelector:
2. – performSelector:withObject:
3. – performSelector:withObject:withObject:
第一个根本就没有参数;第二个,一个论点;第三,两个论点。
答案 3 :(得分:0)
您只能使用performSelector
传递一个参数 [self performSelector:@selector(aSelector) withObject:(id)object];
答案 4 :(得分:0)
NSObject有这些:
- (id)performSelector:(SEL)aSelector withObject:(id)anObject
和
- (id)performSelector:(SEL)aSelector withObject:(id)anObject withObject:(id)anotherObject
您始终可以将anObject设置为NSArray,NSDictionary或其他包含参数的自定义类
答案 5 :(得分:0)
对于多个对象 - 只需使用对象创建数组/字典并将此数组/字典传递给
[self performSelector:@selector(aSelector) withObject:myArray];