倒计时器 [英] Countdown Timer

查看:95
本文介绍了倒计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个倒计时计时器,它倒计时,一个IBOutlet连接到一个文本字段,从60秒到0.我不确定



一个。如何将重复次数限制为60和



B。如何提前递减倒计时Timer:

   - (IBAction)startCountdown:(id)sender 
{
NSTimer * countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(advanceTimer :) userInfo:nil repeats:YES];
NSRunLoop * runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:countdownTimer forMode:NSDefaultRunLoopMode];
}

- (void)advanceTimer:(NSTimer *)timer
{
[countdown setIntegerValue:59];
}


解决方案

跟踪到目前为止。



使用你已经拥有的代码,下面是 advanceTimer 工作:

   - (void)advanceTimer:(NSTimer *)timer 
{
[countdown setIntegerValue: ([countdown integerValue] - 1)];
if([countdown integerValue] == 0)
{
//停止计时器的代码
}
}

edit:
使整个事物更加面向对象,避免从字符串转换为数字并且每次都会返回,我会这样做:

  // Controller.h:
@interface Controller
{
int counter;
IBOutlet NSTextField * countdownField;
}
@property(assign)int counter;
- (IBAction)startCountdown:(id)sender;
@end

p>

  // Controller.m:
@implementation控制器

- (IBAction)startCountdown: id)sender
{
counter = 60;

NSTimer * countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(advanceTimer :)
userInfo:nil
重复:YES ];
}

- (void)advanceTimer:(NSTimer *)timer
{
[self setCounter:(counter -1)
[countdownField setIntegerValue:counter];
if(counter< = 0){[timer invalidate]; }
}

@end

使用绑定,您可以简单地将文本字段的 intValue 绑定到计数器属性> Controller 。这将允许您在类接口中淘汰 IBOutlet ,并在 setIntegerValue:行> advanceTimer 。



update:删除将计时器添加到运行循环两次的代码。



更新:使用 setIntegerValue 在(void)advanceTimer:(NSTimer *)定时器的定义中输入错误...引起恼人的无法识别的选择器发送到实例异常


I'm trying to create a countdown timer that takes countdown, an IBOutlet connected to a textfield, from 60 seconds down to 0. I'm not sure

A. How to limit the repeats to 60 and

B. How to decrement the countdown in advanceTimer:

- (IBAction)startCountdown:(id)sender
{
    NSTimer *countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self     selector:@selector(advanceTimer:) userInfo:nil repeats:YES];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:countdownTimer forMode:NSDefaultRunLoopMode];
}

- (void)advanceTimer:(NSTimer *)timer
{
    [countdown setIntegerValue:59];
}

解决方案

You're on the right track so far.

Sticking with the code you already have, here is how advanceTimer method should look to make it work:

- (void)advanceTimer:(NSTimer *)timer
{
    [countdown setIntegerValue:([countdown integerValue] - 1)];
    if ([countdown integerValue] == 0)
    {
        // code to stop the timer
    }
}

edit: To make the whole thing more object-oriented, and to avoid converting from strings to numbers and back every time, I would instead do something like this:

// Controller.h:
@interface Controller
{
    int counter;
    IBOutlet NSTextField * countdownField;
}
@property (assign) int counter;
- (IBAction)startCountdown:(id)sender;
@end

// Controller.m:
@implementation Controller

- (IBAction)startCountdown:(id)sender
{
    counter = 60;

    NSTimer *countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                         target:self
                                       selector:@selector(advanceTimer:)
                                       userInfo:nil
                                        repeats:YES];
}

- (void)advanceTimer:(NSTimer *)timer
{
    [self setCounter:(counter -1)];
    [countdownField setIntegerValue:counter];
    if (counter <= 0) { [timer invalidate]; }
}

@end

And, if you can make use of bindings, you could simply bind the text field's intValue to the counter property of the Controller. This would allow you to elminate the IBOutlet in the class interface, and the setIntegerValue: line in advanceTimer.

update: Removed the code which adds the timer to the run loop twice. Thank you to Nikolai Ruhe and nschmidt for noticing that error.

update: Used the setIntegerValue method to simplify the code, as per nschmidt.

edit: Typo in definition of (void)advanceTimer:(NSTimer *)timer ... caused annoying 'unrecognized selector sent to instance' exception

这篇关于倒计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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