#swift #xcode
#swift #xcode
Вопрос:
xcode10.1 swift4.2
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.viewWillEnterForeground(_:)), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
}
Раньше я использовал приведенный выше код для запуска метода, когда приложение выходит на передний план. Однако этот код больше не выполняется. Нет UIApplicationWillEnterForeground после NSNotification.Name .
Кто-нибудь знает, как добавить метод уведомления, когда приложение переходит на передний план в VC?
Ответ №1:
Добавьте наблюдателя уведомлений в viewDidLoad
контроллера представления
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.viewWillEnterForeground(_:)), name: UIApplication.willEnterForegroundNotification, object: nil)
}
@objc func viewWillEnterForeground(_ notification: Notification) {
//Work anything app enter in background
}
Сбой после уведомления от делегата приложения willEnterForgroundMethod
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
NotificationCenter.default.post(name: UIApplication.willEnterForegroundNotification, object: nil)
}
Комментарии:
1. Если ваша проблема решена, примите ответ и проголосуйте за. Спасибо