NSObject
方法performSelector:withObject:afterDelay:
允许我在一段时间后使用对象参数调用对象上的方法。它不能用于具有非对象参数的方法(例如整型、浮点型、结构型、非对象指针等)。
使用带有非对象参数的方法来实现相同的功能,最简单的方法是什么?我知道对于常规的performSelector:withObject:
,解决方案是使用NSInvocation
(顺便说一句,这真的很复杂)。但我不知道如何处理“延迟”部分。
谢谢,
发布于 2009-11-14 17:58:50
以下是我过去使用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];
发布于 2009-05-24 11:54:11
只需在NSNumber中包装浮点型、布尔型、整型或类似的类型即可。
对于结构,我不知道有什么方便的解决方案,但是您可以创建一个单独的ObjC类来拥有这样的结构。
发布于 2011-10-21 15:26:55
请勿使用此答案。我离开它只是为了历史的目的。请参阅下面的评论。
如果它是BOOL参数,有一个简单的技巧。
传递nil表示NO,传递self表示YES。将nil转换为BOOL值NO。self被强制转换为BOOL值YES。
如果它不是BOOL参数,这种方法就会失效。
假设self是一个UIView。
//nil will be cast to NO when the selector is performed
[self performSelector:@selector(setHidden:) withObject:nil afterDelay:5.0];
//self will be cast to YES when the selector is performed
[self performSelector:@selector(setHidden:) withObject:self afterDelay:10.0];
https://stackoverflow.com/questions/904515
复制相似问题