viewWillEnterForeground问题

时间:2019-03-08 09:35:45

标签: 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)
           }

当应用程序进入前台时,我曾经使用上面的代码来运行方法。但是,此代码不再运行。 NSNotification.Name之后没有UIApplicationWillEnterForeground。

任何人都知道当应用程序在VC中进入前台时如何添加通知方法吗?

2 个答案:

答案 0 :(得分:0)

在视图控制器的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 :(得分:0)

使用UIApplication.willEnterForegroundNotification代替NSNotification.Name.UIApplicationWillEnterForeground