[英]How to access the remaining time of a non repeating NSTimer
我正在尝试访问NSTimer X的剩余时间。我想每秒更新一个按钮的标题以反映剩余的mm:ss
直到零。 我在这里找不到任何东西。
例如: [btY setTitle:[What to insert here?] forState:UIControlStateSelected];
或者你愿意以不同的方式解决这个问题吗?
你可以使用fireDate
NSTimer *timer = [NSTimer timerWithInterval:1.0 target:self selector:@selector(updateButton) userInfo:nil repeats:YES];
- (void)updateButton:(NSTimer*)timer
{
float timeRemaining = timer.fireDate.timeIntervalSinceNow;
// Format timeRemaining into your preferred string form and
// update the button text
}
这通常不是你如何解决这个问题。
创建一个重复的NSTimer设置为您要更新按钮的分辨率。
因此,例如,如果您希望按钮每秒更改一次,则创建一个NSTimer,如下所示:
NSTimer *timer = [NSTimer timerWithInterval:1.0 target:self selector:@selector(updateButton) userInfo:nil repeats:YES];
然后实现updateButton
; 基本上有一个剩余秒的计数器,每次调用updateButton
时,将计数器减1,并更新按钮的标题。
您将无法获得此类信息,而是需要多次运行计时器,例如,如果您希望每30秒更新一次带有文本的按钮,而不是以30秒启动计时器以1秒钟启动计时器并重复30次
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.