如何使用NSTimer在Objective-C中每隔x秒调用一个方法?

Roe*_*Mik 23 cocoa-touch objective-c nstimer

我正在使用Objective-C,Xcode 4.5.1并开发适用于iPhone的应用程序.

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

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

我只是想每隔x秒调用一次方法B并运行它的计算,但NSTimer要求我提供一些我不确定我应该告诉它的东西.

[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval) 
target:(id) select:(SEL) userInfo:(id) repeats:(BOOL)];
Run Code Online (Sandbox Code Playgroud)

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

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

使用未声明的标识符'self'

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

所以,我self认为' '应该是指向对象的指针,但我想指向哪里?

以下是我的代码的简化:

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

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

MethodB()
{
//Do calculations.
}
Run Code Online (Sandbox Code Playgroud)

如果有人能给我一些答案/指出我正确的方向,我将不胜感激!(:

Her*_*ker 41

Target是select中指定的消息的收件人.在Objective-C中,不调用函数.相反,消息发送到对象.Object在内部引用其符号表,并确定调用哪个方法.那是一个选择器.你的选择器是@selector(MethodB).(顺便说一下:你应该用小写字母开始方法名称."methodB"在这里更合适.)这导致了一个问题:如何确定消息发送到的对象?这是目标.在你的情况下,它很简单self.

BTW:在这种情况下,期望选择器返回void并接受一个id,它是NSTimer对象本身的id.如果您希望计时器根据您的程序逻辑根据某些条件停止触发,那么这将非常方便.最重要的是:你的选择器methodB:不是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.
}
Run Code Online (Sandbox Code Playgroud)


NAN*_*NAV 6

试试这个

 NSTimer *aTimer = [NSTimer timerWithTimeInterval:(x) target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];

    NSRunLoop *runner = [NSRunLoop currentRunLoop];
    [runner addTimer:aTimer forMode: NSDefaultRunLoopMode];  
    [popUpImageView release];

- (void)timerFired:(NSTimer*)theTimer 
{
if(condition)
{
  [theTimer isValid]; //recall the NSTimer
   //implement your methods
}
else
{
  [theTimer invalidate]; //stop the NSTimer

}

}
Run Code Online (Sandbox Code Playgroud)


rck*_*nes 5

那么你试图调用一个普通的C方法,NSTimer不能这样.

目标是要调用选择器的类的实例,此选择器不能选择.此处的选择器是SEL您可以使用该@selector(METHOD_NAME)函数创建的类型 .

例如,这将调用handleTimer :0.1秒:(对于此示例,使用AppDelegate):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //SNIP, some code to setup the windos.   

    [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
    return YES;
}

- (void) handleTimer:(NSTimer *)timer {
    // Hanlde the timed event.
}
Run Code Online (Sandbox Code Playgroud)


Cod*_*Bro 5

如果您查看您的代码并与下面的代码进行比较

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

self 表示您正在类的同一实例中调用方法,在您的示例中,该方法是 myVolumeMonitor

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:@selector(MethodB) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)

不过你很高兴去

方法应该是这样的

- (void)MethodB:(NSTimer*)timer { 
// do something
}
Run Code Online (Sandbox Code Playgroud)