Geo*_*lly 3 cocoa multithreading objective-c
我想打电话给(void)setDoubleValue:(double)value使用performSelectorOnMainThread:.
我认为会起作用的是:
NSNumber *progress = [NSNumber numberWithDouble:50.0];
[progressIndicator performSelectorOnMainThread:@selector(setDoubleValue:)
withObject:progress
waitUntilDone:NO];
Run Code Online (Sandbox Code Playgroud)
没工作.
然后我实现了一个辅助方法:
- (void)updateProgressIndicator:(NSNumber *)progress
{
[progressIndicator setDoubleValue:[progress doubleValue]];
}
Run Code Online (Sandbox Code Playgroud)
工作,但不是很干净.
之后我尝试了NSInvocation.
NSInvocation *setDoubleInvocation;;
SEL selector = @selector(setDoubleValue:);
NSMethodSignature *signature;
signature = [progressIndicator methodSignatureForSelector:selector];
setDoubleInvocation = [NSInvocation invocationWithMethodSignature:signature];
[setDoubleInvocation setSelector:selector];
[setDoubleInvocation setTarget:progressIndicator];
double progress = 50.0;
[setDoubleInvocation setArgument:&progress atIndex:2];
[setDoubleInvocation performSelectorOnMainThread:@selector(invoke)
withObject:nil
waitUntilDone:NO];
Run Code Online (Sandbox Code Playgroud)
这个解决方案有效,但它使用了大量代码并且速度很慢.(即使我存储了调用.)
还有其他方法吗?
如果您使用Snow Leopard,则可以使用Blocks:
dispatch_async(dispatch_get_main_queue(), ^{
[progressIndicator setDoubleValue: 50.0];
});
Run Code Online (Sandbox Code Playgroud)
你需要编写一个自定义的un-boxing方法来包装setDoubleValue:.
- (void) setDoubleValueAsNumber: (NSNumber *) number {
[self setDoubleValue: [number doubleValue]];
}
| 归档时间: |
|
| 查看次数: |
3492 次 |
| 最近记录: |