我想知道有没有一种方式或方法可以用来操作一段代码,当应用程序从后台调用并激活时。我需要从 View 本身而不是应用程序委托(delegate)中工作的方法
最佳答案
当您需要在 View Controller 中执行某些操作以从后台唤醒时,您必须使用 UIApplicationDidBecomeActiveNotification 的监听器
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(self.executeMethod), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}
func executeMethod() {
// execute your code here
}
关于ios - 从特定的 UIViewController 识别 applicationDidBecomeActive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45795913/