首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将performSelector: with primitive :afterDelay:与Cocoa中的原始类型一起使用?

如何将performSelector: with primitive :afterDelay:与Cocoa中的原始类型一起使用?
EN

Stack Overflow用户
提问于 2009-05-24 19:46:18
回答 12查看 143.2K关注 0票数 97

NSObject方法performSelector:withObject:afterDelay:允许我在一段时间后使用对象参数调用对象上的方法。它不能用于具有非对象参数的方法(例如整型、浮点型、结构型、非对象指针等)。

使用带有非对象参数的方法来实现相同的功能,最简单的方法是什么?我知道对于常规的performSelector:withObject:,解决方案是使用NSInvocation (顺便说一句,这真的很复杂)。但我不知道如何处理“延迟”部分。

谢谢,

EN

回答 12

Stack Overflow用户

回答已采纳

发布于 2009-11-15 01:58:50

以下是我过去使用NSInvocation无法更改的名称:

代码语言:javascript
复制
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];
票数 72
EN

Stack Overflow用户

发布于 2009-05-24 19:54:11

只需在NSNumber中包装浮点型、布尔型、整型或类似的类型即可。

对于结构,我不知道有什么方便的解决方案,但是您可以创建一个单独的ObjC类来拥有这样的结构。

票数 35
EN

Stack Overflow用户

发布于 2011-10-21 23:26:55

请勿使用此答案。我离开它只是为了历史的目的。请参阅下面的评论。

如果它是BOOL参数,有一个简单的技巧。

传递nil表示NO,传递self表示YES。将nil转换为BOOL值NO。self被强制转换为BOOL值YES。

如果它不是BOOL参数,这种方法就会失效。

假设self是一个UIView。

代码语言:javascript
复制
//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];
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/904515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档