ios - 检测 applicationWillEnterForeground 中存在哪个 View

标签 ios swift

我想知道当应用程序进入前台时出现哪个 View 。

这怎么可能?

func applicationWillEnterForeground(_ application: UIApplication) {

            if (storyboardID == "myview") {

               //do sth

            }

        }

最佳答案

func getCurrentViewController() -> UIViewController? {

      if let rootController = UIApplication.shared.keyWindow?.rootViewController {
      var currentController: UIViewController! = rootController
         while (currentController.presentedViewController != nil) {
                currentController = currentController.presentedViewController
            }
            return currentController
        }
        return nil
    }

使用上面的代码你将获得当前的View Controller。

使用示例

func applicationWillEnterForeground(_ application: UIApplication) {
    if let storyboardID = getCurrentViewController()?.restorationIdentifier, 
           storyboardID == "myview") {
           //do sth
        }
    }

关于ios - 检测 applicationWillEnterForeground 中存在哪个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49999572/

相关文章:

ios - Swift:方法不知道它接受一个字符串

ios - 索引的分段控件的特定 View 或标签

ios - 取消 DialogViewController 中的 EntryElement 编辑

swift - 我的应用程序可以检测到它是由 Siri 打开的吗?

ios - UIColor 获取色调 :saturation:brightness:alpha: returns NO

ios - 动态添加内容到 UICollectionView

swift - 什么是提供的存储桶?

swift - 设计可观察对象

ios - cellForItemAt indexPath 单元格下载数据的问题

iphone - 如何更改 iPhone 中导航栏的默认高度