swift - 刷新 applicationWillEnterForeground 上的 UILabel

标签 swift appdelegate

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

但是,如果我在应用程序的委托(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/

相关文章:

ios - 只需单击启动图标,应用程序就会卡住我的整个手机

ios - 快速实例化 UIViewController

swift 3向下转换为动态类

ios - 制作 UIApplication(AppDelegate) 嵌入对象

swift - 警告 : Attempt to Present ViewController on whose ViewController isn't in the view hierarchy (3D Touch)

ios - 应用程序委托(delegate)检查所有 View Controller 转换何时完成

ios - 如何将rightBarButtonItem传递给TabBarController中的所有ViewController?

ios - 在 iOS 中为 Today Extension(Widget) 安装 pods

ios - 在可能的 block /闭包保留周期中获取 swift_unknownWeakRelease()

swift 3.0.2 给出错误 '' Type 'bool' is broken"