如何在NSTimer中显示标签中的秒数?
imageVC.h
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
imageVC.m
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
imageViewController *pickerView = [[imageViewController alloc] init];
[NSTimer scheduledTimerWithTimeInterval:10
target:self
selector:@selector(timeout)
userInfo:nil
repeats:NO];
}];
}
}
任何帮助都会非常感激。
感谢
答案 0 :(得分:1)
尝试将timeOut值设置为您想要的最大值。
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeout) userInfo:nil repeats:YES];
-(void) timeout {
if (timeCount==0) {
[timer invalidate];
} else {
timeCount = timeCount-1;
timeLabel.text = [NSString stringWithFormat:@"%d",timeCount];
}
}
答案 1 :(得分:0)
添加另一个计时器,间隔为1秒,更新标签
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
imageViewController *pickerView = [[imageViewController alloc] init];
[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(timeout) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setTimeToLabel) userInfo:nil repeats:NO];
}
- (void)setTimeToLabel
{
counterPrivateVar += 1;
self.lbl.text = [NSString stringWithFormat:@"%d", counter];
}