iphone - 如何在 iOS 上定期(每秒)更新标签?

标签 iphone uikit

我使用 NSTimer,它每秒触发一次并更新一个标签,该标签显示事件发生之前的剩余时间。

到目前为止我工作得很好。问题是当我滚动 TableView 时,我的标签没有更新,因为主线程被触摸/滚动事件阻止。

我考虑过为计时器创建第二个线程,但无论如何我都无法从后台线程更新标签。我必须在 MainThread 上使用 PerformSelector... 对它进行排队,它会像以前一样卡住。

有什么方法可以在滚动时更新标签吗?

最佳答案

问题是当主线程跟踪触摸时,不会调用 ScheduledTimer。您需要在主运行循环中安排计时器。

所以而不是这样做

[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES];

使用

NSTimer* timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

关于iphone - 如何在 iOS 上定期(每秒)更新标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6229759/

相关文章:

ios - MFMailComposeViewController未在iOS 5中填充收件人

iphone - 在 iPhone 应用程序升级上部署 sqlite DB

iphone - 错误 : expected specifier-qualifier-list before 'cv'

ios - SwiftUI 中 NavigationView 中后退按钮的默认文本

java - 加密/解密 - iphone 到 java - BadPaddingException : Given final block not properly padded

ios - iOS 8 和 9 中 View 的圆角半径是多少?

ios - @synchronized [UIView animationWithDuration ...] 锁定结束?

iphone - 防止编辑时 UITableViewCell (contentView) 缩进

ios - 向 UIBezierPath 动态添加值

iphone - 连结应用程式时发生错误