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

mem*_*cal 10 generics ios swift swiftui uihostingcontroller

我有一个 UIHostingController,它托管一个名为 CatalogView 的 SwiftUI 视图。显示它时,会附加一个环境对象,因此基本上从 UIKit 来看,它显示如下:

let rootCatalogView = CatalogView()

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

navigationController.pushViewController(catalogView, animated: true)
Run Code Online (Sandbox Code Playgroud)

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

type(of:) 显示以下内容,这是有意义的:

UIHostingController<ModifiedContent<CatalogView, _EnvironmentKeyWritingModifier<Optional<AppState>>>>
Run Code Online (Sandbox Code Playgroud)

诸如 vc.self is UIHostingController.Type 或 vc.self is UIHostingController< CatalogView >.Type 之类的东西都返回 false (vc 是 navigationController.viewControllers 的元素

以下显然有效,它返回 true,但是 UIHostingController 初始化中的任何更改都会更改其类型

vc.isKind(of: UIHostingController<ModifiedContent<CatalogView, _EnvironmentKeyWritingModifier<Optional<StoreManager>>>>.self)
Run Code Online (Sandbox Code Playgroud)

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

Dan*_*ith 11

是的,您不能转换为泛型类,但您可以声明协议并仅为 UIHostingViewController 实现它

import UIKit
import SwiftUI

private protocol AnyUIHostingViewController: AnyObject {}
extension UIHostingController: AnyUIHostingViewController {}

extension UIViewController {
   var isHosting: Bool { self is AnyUIHostingViewController } 
}
Run Code Online (Sandbox Code Playgroud)

以及代码中的某处

func someFunction(viewController: UIViewController) {
  if viewController.isHosting {
     print("That is hosting view controller")
  }
}
Run Code Online (Sandbox Code Playgroud)


And*_*rew 5

由于通用参数,我们无法在UIHostingController不知道完整约束的情况下强制转换 ViewController 来查找它是否是 a。

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

UIHostingController是 的子类,UIViewController所以我们可以执行以下操作。

在其上创建一个计算属性,UIViewController该属性返回用于创建的类的名称UIViewControllerViewControllers这为我们提供了在包含的列表中搜索的内容UINavigationController

extension UIViewController {
    var className: String {
        String(describing: Self.self)
    }
}
Run Code Online (Sandbox Code Playgroud)

创建一些 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"))
Run Code Online (Sandbox Code Playgroud)

然后我们可以将它们添加到UINavigationController.

let nav = UINavigationController(rootViewController: first)
nav.pushViewController(second, animated: false)
nav.pushViewController(hosting, animated: false)
nav.pushViewController(myHosting, animated: false)
Run Code Online (Sandbox Code Playgroud)

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

for vc in nav.viewControllers {
    print(vc.className)
}
Run Code Online (Sandbox Code Playgroud)

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

第一个视图控制器

第二个视图控制器

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)
}
Run Code Online (Sandbox Code Playgroud)

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