刷新 applicationWillEnterForeground 上的 UILabel

Refreshing UILabel on applicationWillEnterForeground

我试图在应用程序从后台状态返回时刷新标签,但是我找不到引用该标签的方法。

但是,如果我在应用程序委托的 applicationWillEnterForeground 下添加 label.text = titles[emotionOfTheDay],它会正确地告诉我它不知道我在说什么标签,因为标签在我的 ViewController。有没有办法从不同的文件中引用这个标签,还是我用错了方法?

谢谢。

你需要实施

override func viewDidLoad() {
    NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

}

deinit { 
    NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
}

@objc fileprivate func willEnterForeground() {
    // refresh the label here 
}

这是我的较短版本:

NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: .main) { _ in
    //Do stuff here...
}

将在例如 viewDidLoad()

中实现