如何检查视图控制器是否属于 UIHostingController 类型

问题描述

我有一个 UIHostingController,它托管一个名为 CatalogView 的 SwiftUI 视图。 当显示它时,附加了一个环境对象,所以基本上从 UIKit 它显示如下:

let rootCatalogView = CatalogView()

let appState = AppState.get()
let catalogView = UIHostingController(rootView: rootCatalogView.environmentObject(appState))

navigationController.pushViewController(catalogView,animated: true)

现在稍后我需要检查这个 UIHostingController 是否在 navigationController.viewControllers 的列表中

type(of:) 显示以下内容,哪种有意义:

UIHostingController<ModifiedContent<CatalogView,_EnvironmentKeyWritingModifier<Optional<AppState>>>>

像 vc.self 是 UIHostingController.Type 或 vc.self 是 UIHostingController.Type 之类的东西都返回 false(vc 是 navigationController.viewControllers 的一个元素

以下显然有效,它返回真,但 UIHostingController 初始化的任何变化都会改变它的类型

vc.isKind(of: UIHostingController<ModifiedContent<CatalogView,_EnvironmentKeyWritingModifier<Optional<StoreManager>>>>.self)

如何检查视图控制器是否为 UIHostingController 类型? 或者至少我如何将控制器投射到 UIHostingController 以便我可以检查它的根视图?

解决方法

由于泛型参数的事实,我们无法在不知道完整约束的情况下强制转换 ViewController 以查找它是否为 UIHostingController

我应该注意到,这不是一个理想的解决方案,它实际上只是一种解决方法。

UIHostingControllerUIViewController 的子类,因此我们可以执行以下操作。

UIViewController 上创建一个计算属性,该属性返回用于创建 UIViewController 的类的名称。这使我们可以在 ViewControllers

中包含的 UINavigationController 列表中搜索一些内容
extension UIViewController {
    var className: String {
        String(describing: Self.self)
    }
}

创建一些 UIViewController 子类和我们的 UIHostingController

class FirstViewController: UIViewController {}
class SecondViewController: UIViewController {}
class MyHostingController<Content>: UIHostingController<Content> where Content : View {}

let first = FirstViewController()
let second = SecondViewController()
let hosting = UIHostingController(rootView: Text("I'm in a hosting controller"))
let myHosting = MyHostingController(rootView: Text("My hosting vc"))

然后我们可以将这些添加到 UINavigationController

let nav = UINavigationController(rootViewController: first)
nav.pushViewController(second,animated: false)
nav.pushViewController(hosting,animated: false)
nav.pushViewController(myHosting,animated: false)

现在我们的 UINavigationController 中有一些 ViewController,我们现在可以遍历它们并找到一个 ViewController,它的 className 包含我们正在寻找的内容。

for vc in nav.viewControllers {
    print(vc.className)
}

这会将以下内容打印到控制台:

FirstViewController

第二个视图控制器

UIHostingController

MyHostingController

然后您可以 for-where 在层次结构中找到 ViewController。

for vc in nav.viewControllers where vc.className.contains("UIHostingController") {
    // code that should run if its class is UIHostingController
    print(vc.className)
}

for vc in nav.viewControllers where vc.className.contains("MyHostingController") {
    // code that should run if its class is MyHostingController
    print(vc.className)
}

正如我上面所说,这不是一个理想的解决方案,但它可能会帮助您,直到有更好的转换方式而不知道通用约束。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...