是否可以刷新Today Widget中的计时器? [英] Is it possible to refresh a timer in a Today Widget?

查看:79
本文介绍了是否可以刷新Today Widget中的计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在今天的小部件中更新计时器的文本标签。
我看了一下,但没有任何帮助。

I was wondering is it possible to update the text label of a timer in a today widget. I took a look around but nothing helped me.

推荐答案

是的,你可以。我刚刚测试过,它有效。您只需将计时器添加到主运行循环NSRunLoopCommonModes:

Yes you can. I have just tested and it works. You just have to add your timer to the main run loop 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)
    }
}

这篇关于是否可以刷新Today Widget中的计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆