我想知道是否可以在今天的小部件中更新计时器的文本标签。我环顾四周,但没有任何帮助。
问问题
4430 次
2 回答
13
是的你可以。我刚刚测试过,它可以工作。您只需将计时器添加到主运行循环 NSRunLoopCommonModes:
RunLoop.main.add(yourTimerName, forMode: .commonModes)
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding {
@IBOutlet weak var strTimer: UILabel!
var timer = Timer()
func updateInfo() {
strTimer.text = Date().description
}
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateInfo), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: .commonModes)
}
func widgetPerformUpdate(completionHandler: @escaping (NCUpdateResult) -> Void) {
completionHandler(.newData)
}
}
于 2015-01-19T06:03:44.077 回答
4
我知道这是一个 Swift 问题,但我发现它正在寻找 Objective-C 代码,其他人也可能如此。
-(void) viewDidLoad
{
[NSTimer scheduledTimerWithTimeInterval:1 // update more than once a second to appear in sync with the system clock
target:self
selector:@selector(updateUi:)
userInfo:nil
repeats:YES];
}
-(void) updateUi:(NSTimer *)timer
{
// Update Widget UI as required
}
于 2015-09-20T19:18:47.520 回答