如何使用nativeC类型与performSelectorOnMainThread:? [英] How to use native C types with performSelectorOnMainThread:?

查看:136
本文介绍了如何使用nativeC类型与performSelectorOnMainThread:?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 performSelectorOnMainThread:调用(void)setDoubleValue:(double)value p>

我认为会工作的是:

  NSNumber * progress = numberWithDouble:50.0]; 
[progressIndicator performSelectorOnMainThread:@selector(setDoubleValue :)
withObject:progress
waitUntilDone:NO];

无法使用。



然后我实现了一个辅助方法:

   - (void)updateProgressIndicator:(NSNumber *)progress 
{
[progressIndicator setDoubleValue:[progress doubleValue]];
}

p>

之后,我尝试使用 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];

此解决方案适用,但它使用了大量代码,速度相当慢。 (即使我存储调用。)



有其他方法吗?

解决方案

如果你在Snow Leopard,你可以使用Blocks:

  dispatch_async(dispatch_get_main_queue ,^ {
[progressIndicator setDoubleValue:50.0];
});


I'd like to call (void)setDoubleValue:(double)value using performSelectorOnMainThread:.

What I thought would work is:

NSNumber *progress = [NSNumber numberWithDouble:50.0];
[progressIndicator performSelectorOnMainThread:@selector(setDoubleValue:)
                                    withObject:progress
                                 waitUntilDone:NO];

Didn't work.

Then I implemented a helper method:

- (void)updateProgressIndicator:(NSNumber *)progress
{
    [progressIndicator setDoubleValue:[progress doubleValue]];
}

Works, but not really clean.

After that I tried it with 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];

This solution works, but it uses a lot of code and is quite slow. (Even if I store the invocation.)

Is there any other way?

解决方案

If you are on Snow Leopard, you can use Blocks:

dispatch_async(dispatch_get_main_queue(), ^{
    [progressIndicator setDoubleValue: 50.0];
});

这篇关于如何使用nativeC类型与performSelectorOnMainThread:?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆