每当应用程序从后台状态返回时,我都会尝试刷新标签,但是我找不到引用该标签的方法。
但是,如果我在应用程序的委托(delegate)中的 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
}
关于swift - 刷新 applicationWillEnterForeground 上的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50788998/