2011-08-12 29 views

回答

2
+0

使用[链接]( http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/performSelector:withObject:withObject :) ' - (id)performSelector:(SEL)aSelector withObject:(id)anObject withObject:(id)anotherObject'你可以传递两个对象 - 我不知道是否有可能通过更多。 – SideSwipe

+0

好的多谢了 –

+0

正如其他答案中所述,您可以通过将它们放在NSDictionary或NSArray中传递给方法,但您必须更改方法,传递给做这个。 – SideSwipe

0

可以传递只有一个参数与performSelector

[self performSelector:@selector(aSelector) withObject:(id)object]; 
0

NSObject的有以下:

- (id)performSelector:(SEL)aSelector withObject:(id)anObject 

- (id)performSelector:(SEL)aSelector withObject:(id)anObject withObject:(id)anotherObject 

你总是可以让anObject是一个NSArray,NSDictionary的或其他自定义类,将包含参数为你

1

你有三种可能性,整体:

1. – performSelector: 
2. – performSelector:withObject: 
3. – performSelector:withObject:withObject: 

第一个根本没有论据;第二,一个论点;第三,两个论点。

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:targetObjectMyClass类型:

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]; 
+0

请告诉我如何通过超过参数使用NSInvocation –

+0

我添加了一些示例代码。希望它会有用! – Anton

+0

非常感谢安东 –

0

多个对象 - 只需创建你的对象数组/字典,并通过这个数组/词典

[self performSelector:@selector(aSelector) withObject:myArray]; 
相关问题