簡體   English   中英

如何以編程方式打開/關閉計時器

[英]How to toggle on/off timer programmatically

這就是我想要做的:我想要一個計時器,觸發一個方法然后,在這個方法的最后,切換關閉,並在另一個方法上打開另一個計時器,然后進入一個循環。

那么用於在方法上打開和關閉計時器的代碼是什么?

在Delphi中我使用:

timer.enable:=True; // timer.enable:=False;

在objective-c上有類似的方法嗎? 我正在使用Xcode 4.4謝謝!

要關閉計時器,請在計時器上調用invalidate ,如下所示:

[yourTimer invalidate]

然后開始一個新的:

NSTimer *newTimer;

                     newTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 //Every how many seconds
                                                                target:self
                                                              selector:@selector(methodToCall)
                                                              userInfo:nil
                                                               repeats:YES];

假設您的NSTimer被稱為“計時器”,您可以使用......

[timer invalidate]

停止計時器。 要使計時器立即將消息傳遞給它的目標方法,請使用

[timer fire]

要啟動計時器,請使用文檔中列出的構造函數方法之一(https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html)如

NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(doThisWhenTimerFires:) userInfo:nil repeats:NO]

- (void)doThisWhenTimerFires:(NSTimer *)timer
{
     //code here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM