2011-08-12 30 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]; 
相關問題