如何使用performSelector:withObject:afterDelay:with Cocoa中的原语类型? [英] How to use performSelector:withObject:afterDelay: with primitive types in Cocoa?
问题描述
NSObject
方法 performSelector:withObject:afterDelay:
允许我调用对象上的方法参数在一定时间后。它不能用于具有非对象参数的方法(例如,int,float,structs,非对象指针等)。
The NSObject
method performSelector:withObject:afterDelay:
allows me to invoke a method on the object with an object argument after a certain time. It cannot be used for methods with a non-object argument (e.g. ints, floats, structs, non-object pointers, etc.).
最简单的方法来实现与非对象参数的方法相同的事情?我知道对于常规 performSelector:withObject:
,解决方案是使用 NSInvocation
(顺便说一句, )。但我不知道如何处理延迟部分。
What is the simplest way to achieve the same thing with a method with a non-object argument? I know that for regular performSelector:withObject:
, the solution is to use NSInvocation
(which by the way is really complicated). But I don't know how to handle the "delay" part.
谢谢,
推荐答案
这里是我用来调用的东西我不能改变使用NSInvocation:
Here is what I used to call something I couldn't change using NSInvocation:
SEL theSelector = NSSelectorFromString(@"setOrientation:animated:");
NSInvocation *anInvocation = [NSInvocation
invocationWithMethodSignature:
[MPMoviePlayerController instanceMethodSignatureForSelector:theSelector]];
[anInvocation setSelector:theSelector];
[anInvocation setTarget:theMovie];
UIInterfaceOrientation val = UIInterfaceOrientationPortrait;
BOOL anim = NO;
[anInvocation setArgument:&val atIndex:2];
[anInvocation setArgument:&anim atIndex:3];
[anInvocation performSelector:@selector(invoke) withObject:nil afterDelay:1];
这篇关于如何使用performSelector:withObject:afterDelay:with Cocoa中的原语类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!