applicationWillEnterForeground

applicationWillEnterForeground

我可以在最新版本的Xcode 11.1中使用NSNotification。
从后台返回到应用程序后,我可以尝试使用我的函数refreshFields()更新字段值。
我的代码编译成功,但是函数applicationWillEnterForeground()从未调用。
错误在哪里?

@objc func applicationWillEnterForeground(notification: NSNotification) {
    refreshFields()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear( animated)
    let app = UIApplication.shared
    NotificationCenter.default.addObserver(self, selector: #selector(self.applicationWillEnterForeground(notification:)), name: NSExtensionHostWillEnterForeground, object: app)
}

最佳答案

我找到了解决方案。
通知名称错误。
它必须是:UIApplication.willEnterForegroundNotification

@objc func applicationWillEnterForeground(notification: NSNotification) {
    refreshFields()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear( animated)
    let app = UIApplication.shared
    NotificationCenter.default.addObserver(self, selector: #selector(self.applicationWillEnterForeground(notification:)), name: UIApplication.willEnterForegroundNotification, object: app)
}

10-08 02:51