1

我可以在 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)
}
4

1 回答 1

1

我找到了解决方案。通知名称的错误。必须是: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)
}
于 2019-10-21T19:27:20.987 回答