如何在Objective-C使用NSTimer每x秒调用一个方法? [英] How to call a method every x seconds in Objective-C using NSTimer?

查看:126
本文介绍了如何在Objective-C使用NSTimer每x秒调用一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Objective-C,Xcode 4.5.1和iPhone上的应用程序。

I am using Objective-C, Xcode 4.5.1 and working on an app for the iPhone.

我有一个方法A,方法B每x秒进行一系列计算。在方法A中,我开始播放音频文件。方法B将在音频文件的持续时间内每x秒监视音频。

I have a method A in which I want to call another method B to do a series of calculations every x seconds. In method A I start playing an audio file. Method B will monitor the audio every x seconds for the duration of the audio file.

我发现 NSTimer 一个潜在的解决方案,但我很难让它工作/理解它。

I have found NSTimer as a potential solution, but am having a hard time getting it to work/understanding it.

我只是想每x秒调用方法B,并运行其计算,但 NSTimer 要求我提供几个我不知道应该告诉它的东西。

I simply want to call Method B every x seconds and run its calculations, but NSTimer requires me to provide several things of which I'm not sure what I'm supposed to tell it.

[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval) 
target:(id) select:(SEL) userInfo:(id) repeats:(BOOL)];

我的理解是,在 NSTimeInterval I提供我想要 NSTimer 操作的间隔。但是,我如何告诉它运行方法B?

It is my understanding that at NSTimeInterval I provide the interval at which I want NSTimer to operate. But, how do I tell it to run Method B?

我查看了示例代码,目前我的印象是,我在'选择提供方法:'。但是,我在'目标:'写什么?为什么我需要一个目标?我尝试输入 self ',但Xcode告诉我:

I have looked at example code, and am currently under the impression that I provide the method at the 'select:'. But, what do I write at the 'target:'? Why would I need a target? I tried entering 'self', but Xcode tells me:


使用未声明标识符'self'

Use of undeclared identifier 'self'



[NSTimer scheduledTimerWithTimeInterval:0.1 target:self 
select:@selector(targetMethod:myVolumeMonitor()) userInfo:nil repeats:YES];

因此,我认为' self '

下面是我的代码的简化:

Below is a simplification of my code:

MethodA()
{
//Start playing an audio file.

//NSTimer calling Method B, as long the audio file is playing, every x seconds.
}

MethodB()
{
//Do calculations.
}

如果有人可以向我提供一些答案/正确的方向! (:

I would be grateful if somebody could provide me with some answers/point me in the right direction! (:

推荐答案

Target是在select中命名的消息的接收者
在Objective-C函数中, 。这里有一个选择器 @selector(MethodB)
(BTW:你应该用小写字母开始方法名称。methodB在这里更合适。)
这会导致一个问题:如何确定消息的对象发送?这是目标。在你的情况下,它只是 self

Target is the recipient of the message named in select. In Objective-C functions are not called. There are rather messages sent to objects. The Object internally refers to its symbol table and determines which of its methods is being called. That is a selector. Your selector is @selector(MethodB). (BTW: you should start method names with lower case. "methodB" would be more appropriate here.) This leads to the question: how to determine the object to which the message is sent? That is the target. In your case, it is simply self.

选择器应该返回void并接受一个id,这是一个NSTimer对象本身的id,如果你想要定时器根据你的程序逻辑停止激活
最重要的是:您的选择器为 methodB:,而不是 methodB

BTW: In this case the selector is expected to return void and accept an id, which is the id of the NSTimer object itself. That will come handy if you want the timer to stop firing based on some conditions according to your program logic. Most important: Your selector is then methodB: rather than methodB.

- (void) methodA
{
//Start playing an audio file.

//NSTimer calling Method B, as long the audio file is playing, every 5 seconds.
[NSTimer scheduledTimerWithTimeInterval:5.0f 
target:self selector:@selector(methodB:) userInfo:nil repeats:YES];
}

- (void) methodB:(NSTimer *)timer
{
//Do calculations.
}

这篇关于如何在Objective-C使用NSTimer每x秒调用一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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