是否有可能刷新今日小工具的计时器?

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

是的你可以。 我刚刚testing过,它的工作原理。 您只需将您的计时器添加到主运行循环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) } } 

我知道这是一个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(updateTimes:) userInfo:nil repeats:YES]; } -(void) updateTimes:(NSTimer *)timer { // Update Widget UI as required }