2012-11-19 28 views
18

我正在使用Objective-C,Xcode 4.5.1和iPhone上的應用程序。如何使用NSTimer在Objective-C中每x秒調用一次方法?

我有一個方法A,其中我想調用另一個方法B每x秒執行一系列計算。在方法A中,我開始播放音頻文件。方法B將在音頻文件的持續時間內每隔x秒監視一次音頻。

我發現NSTimer是一個潛在的解決方案,但我很難讓它工作/理解它。

我只是想每x秒調用一次方法B並運行它的計算,但NSTimer要求我提供幾個我不確定我應該告訴它的事情。

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

這是我的理解是在NSTimeInterval我提供的,我想NSTimer操作的時間間隔。但是,我如何告訴它運行方法B?

我看了一下示例代碼,目前我的印象是我在'select:'處提供了這個方法。但是,我在'target:'上寫什麼?爲什麼我需要一個目標?我試着輸入「self」,但是Xcode中告訴我:

使用未聲明的標識符「自我」

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

所以,我想的「self」應該是一個指向對象,但我想指向哪裏?

下面是我的代碼的簡化:

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

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

MethodB() 
{ 
//Do calculations. 
} 

我將不勝感激,如果有人能爲我提供一些答案/點我在正確的方向! (:

回答

33

目標是在選擇指定的郵件的收件人。 不調用Objective-C函數。有消息發送到對象。 Object在內部引用它的符號表並確定它的哪個方法正在被調用。這是一個選擇器。您的選擇是@selector(MethodB)。 (順便說一下:你應該使用小寫啓動方法名,「methodB」在這裏更合適。) 這會導致一個問題:如何確定發送消息的對象?這是目標。在你的情況下,它只是self。在這種情況下,選擇器應該返回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. 
} 
+0

謝謝你的回答!你是一個很好的幫助!我設法弄明白這一切!(:@Hermann Klecker – RoelfMik

+0

不客氣。如果事情有幫助,我不介意'接受'。 :) –

+0

隨着我的鼠標點擊我已經授予你'接受':) – RoelfMik

4

那麼你試圖調用正常的C方法,NSTimer不能到

目標是類上調用selector,這個選擇ADN未選擇的一個實例。這裏的選擇器是一個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. 
} 
+0

謝謝您的回答!我設法弄明白這一切! (: – RoelfMik

4

如果你看一下你的代碼,並與下面

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

自我的一個意味着你正在調用同一實例的方法你的類,在您的示例方法是myVolumeMonitor

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

,你是好去,雖然

方法應該是這樣的

- (void)MethodB:(NSTimer*)timer { 
// do something 
} 
+0

選擇不是方法'scheduledTimerWithTimeInterval'的一部分,方法名稱也應該以小寫字母開始。 – rckoenes

+0

Dunno,從工作應用程序源代碼中複製這個。好吧,是的,我改變了選擇器以嘗試加粗我的答案 – CodeBro

+0

您在示例中調用的方法不存在:['scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:'](https://developer.apple.com/library/IOS/#documentation/Cocoa/Reference/Foundation/Classes /NSTimer_Class/Reference/NSTimer.html#//apple_ref/doc/uid/20000319-CHDDAEIA)這個方法應該是什麼。 – rckoenes

4

試試這個

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 

} 

} 
+0

謝謝你的回答!我設法解決了這個問題!:) – RoelfMik

相關問題