ios - 从特定的 UIViewController 识别 applicationDidBecomeActive

标签 ios swift3

我想知道有没有一种方式或方法可以用来操作一段代码,当应用程序从后台调用并激活时。我需要从 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/

相关文章:

ios - 具有两个 TableView 的 SplitviewController,委托(delegate)问题

android - 苹果开发商导出合规

ios - 为所有按钮使用一个 ViewController xcode

javascript - Titanium Appcelerator - 多屏应用

ios - swift 3 : How to animate a view background color similar to a progress bar

ios - 此处 Maps iOS SDK 问题(Swift3、Xcode 8)的 currentPosition 为零

c - swift3.0 无法将类型 '[UnsafeMutablePointer<Int8>]' 的值转换为预期的参数类型 'UnsafeMutablePointer<Int8>?'

ios - 无法将类型 'Data' 的值转换为预期的参数类型 'Data'

swift3 - 本地通知在模拟器 iOS 10.0 中不起作用

ios - 如何隐藏应用状态栏