0 iphone cocoa-touch objective-c nstimer
我有一个很大的问题但无法解决它:我使用NSTimer进行倒计时,倒计时工作正常,点击一个按钮,倒计时开始,当达到零时,会出现一个标签.当我再次按下按钮并再次开始倒计时时出现问题,但这次快了两倍!越来越快......我不知道该做什么......我需要做一个循环,但只能工作一次......我一直在寻找,但我没有找到任何关于它...不知道如果有人有同样的问题.感谢名单.
NSTimer *timer; int i;
-(IBAction)pressButton{
i = 10;
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timerFires)
userInfo:nil
repeats:YES];
[timer fire];
}
- (void) timerFires{
if(i > 0){
i--;
label.text = [NSString stringWithFormat:@"%i", i];
}
else{
label.text = @"Tiempo!!";
timer = nil;
}
}
Run Code Online (Sandbox Code Playgroud)
您可能多次触发计时器.第二次单击按钮时,需要取消第一个计时器.
-(IBAction)pressButton{
i = 10;
if (timer != nil) {
[timer invalidate];
}
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timerFires)
userInfo:nil
repeats:YES];
[timer fire];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
113 次 |
| 最近记录: |