iOS 的 NSObject對象提供了一種在不同線程中執行其方法的機制。
最常見的是需要在主線程即UI線程中去執行一些方法
performSelectOnMainThread:withObject:waitUntilDone:
但是這個預設的方法只支援一個參數。
performSelector:withObject:withObject:
這個不是在主程線中啟動並執行…只能依靠category來實現
@interface NSObject (PGPerformSelectorOnMainThreadWithTwoObjects)- (void) performSelectorOnMainThread:(SEL)selector withObject:(id)arg1 withObject:(id)arg2 waitUntilDone:(BOOL)wait;@end
@implementation NSObject (PGPerformSelectorOnMainThreadWithTwoObjects)- (void) performSelectorOnMainThread:(SEL)selector withObject:(id)arg1 withObject:(id)arg2 waitUntilDone:(BOOL)wait{NSMethodSignature *sig = [self methodSignatureForSelector:selector];if (!sig) return;NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];[invo setTarget:self];[invo setSelector:selector];[invo setArgument:&arg1 atIndex:2];[invo setArgument:&arg2 atIndex:3];[invo retainArguments];[invo performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:wait];}@end