如何在 Cocoa 中使用 performSelector:withObject:afterDelay: 原始类型? [英] How to use performSelector:withObject:afterDelay: with primitive types in Cocoa?

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

问题描述

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

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];

这篇关于如何在 Cocoa 中使用 performSelector:withObject:afterDelay: 原始类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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