NSTimer 倒计时及更新UI
描述:
使用NSTimer实现倒计时自动登录功能。
解决:
//创建计时器。每秒调用updateUI方法。
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateUI:) userInfo:nil repeats:YES];
-(void)updateUI:(id)sender
{
if((self.time--)>0)
{
NSLog(@"timer:%d",self.time);
//更新UI
self.timeLabel.text = [NSString stringWithFormat:@"%ds后自动跳转",self.time];
//timelabel使用了sizeToFit,这里也要再使用一次。
[self.timeLabel sizeToFit];
if (self.time == 0)
{
//登录
[self pressLoginBtnTouched:self];
}
}
else
{
//倒计时结束 需要关闭计时器
[self.timer invalidate];
}
}