Objective c 按秒进入倒计时
我创建了一个显示在标签上的倒计时计时器,现在我想添加一个函数,当用户按下按钮时,它将向倒计时计时器添加30秒。 我试着自己做,但没有成功(我犯了一个错误,有一次它停止了我的计时器),我怎么做? 这是我的密码:Objective c 按秒进入倒计时,objective-c,xcode,nstimer,countdown,Objective C,Xcode,Nstimer,Countdown,我创建了一个显示在标签上的倒计时计时器,现在我想添加一个函数,当用户按下按钮时,它将向倒计时计时器添加30秒。 我试着自己做,但没有成功(我犯了一个错误,有一次它停止了我的计时器),我怎么做? 这是我的密码: -(IBAction)start:(id)sender { timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateTimer:) userInfo:n
-(IBAction)start:(id)sender
{
timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
}
-(void)updateTimer:(NSTimer *)timer
{
currentTime -= 10 ;
[self populateLabelwithTime:currentTime];
if(currentTime <=0)
{
[timer invalidate];
//some code when countdown reach to 00:00:00
}
}
- (void)populateLabelwithTime:(int)milliseconds {
seconds = milliseconds/1000;
minutes = seconds / 60;
seconds -= minutes * 60;
NSString * countdownText = [NSString stringWithFormat:@"%@%02d:%02d:%02d", (milliseconds<0?@"-":@""), minutes, seconds,milliseconds%1000];
countdownLabel.text = countdownText;
}
-(IBAction)addTime:(id)sender
{
timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(addToCurrent:) userInfo:nil repeats:YES];
}
-(void) addToCurrent:(NSTimer *)timerb;
{
seconds +=10;
[self populateLabelWithTime:seconds];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
currentTime = 2700;
}
-(iAction)开始:(id)发送方
{
计时器=[NSTimer scheduledTimerWithTimeInterval:.01目标:自选择器:@selector(UpdateTime:)用户信息:无重复:是];
}
-(void)UpdateTime:(NSTimer*)计时器
{
当前时间-=10;
[self-populateLabelwithTime:currentTime];
如果(currentTime没有必要在每次单击按钮时重新创建计时器。而且,在我看来,当您尝试将30秒添加回计时器时,您无意中修改了秒而不是currentTime
-(IBAction)addTime:(id)sender
{
currentTime += 30000;
[self populateLabelWithTime:currentTime];
}
准确地包括出错的地方和错误是什么是很有帮助的。-(iAction)addTime:(id)sender{currentTime+=30000;}