0

我正在尝试从用户输入的数字开始倒计时,但我不确定如何做到这一点。我已经对代码进行了多次尝试和更改,但没有任何区别。

我的代码:

-(IBAction)Start 
{
    mainInt = minutes;
    Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown)
    userInfo:nil repeats:YES];
}

我已将“分钟”声明为 UITextField,以便显示代表用户输入的数字是吗?或者没有?

我在 .m 文件中也有 @synthesised *minutes

关于我可以做些什么来使倒计时从输入到 UITextField 的数字开始的任何建议?

4

1 回答 1

0

这个答案应该很好用

-(IBAction)Start 
{
    mainInt = [minutes.text integerValue];
    Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown)
    userInfo:nil repeats:YES];
}

-(void)countDown
{
    mainInt -=1;
    if (mainInt ==0) 
    {
        [Timer invalidate];
    }
    countDownLabel.text = [NSString stringWithFormat:@"%d",mainInt];
}

使用的变量:

  • minute是用户输入数字的 UITextField 对象。
  • mainInt是 NSInteger 类型,保存更新后的计数器值。
  • countDownLabel是 UILabel 的对象,您将使用它来显示更新后的计数器。
于 2013-05-29T09:34:38.590 回答