后计时器

时间:2013-02-10 22:47:39

标签: objective-c cocoa-touch

如果按下按钮,我想要一个正在启动的计时器。 计时器运行时,无法再次按下按钮。并会显示一条警告.. 你知道如何实现它吗? 坦克你!

1 个答案:

答案 0 :(得分:1)

第一次按下按钮时创建一个NSTimer,当计时器有效时,不要让动作发生,并弹出警报。

将NSTimer存储在对象的属性中。

接口:

@property (strong, nonatomic) NSTimer *timer;

实现:

- (IBAction)buttonClicked:(id)sender
{
    if(self.timer == nil || ![self.timer isValid]) {
        // Allow the action (set the timer interval to what suits your needs)
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 invocation:nil repeats:NO];

        // DO THE ACTION HERE!
        NSLog(@"You can do it this time!");
    } else {
        // Deny the action (perhaps popup an alert)
        NSLog(@"Can't do that yet!");
    }
}