您的位置:首页 > 移动开发 > IOS开发

[iOS] performSelectorOnMainThread支持多个参数

2014-04-01 10:11 471 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: