如何检查一个视图控制器是否为UIHostingController类型

9

我有一个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< CatalogView >.Type,两者都返回 false(vc 是 navigationController.viewControllers 的元素)。

明显以下代码可以正常工作,它返回 true,但是 UIHostingController 初始化的任何更改都将更改其类型。

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

如何检查视图控制器是否为UIHostingController类型?或者至少如何将控制器转换为UIHostingController以便可以检查其rootview?

尝试使用 if vc is UIHostingController - aheze
2
UIHostingController是一个带有泛型参数的类,因此不起作用。 - memical
Xcode 给出以下错误提示:无法推断泛型参数“Content”在转换为“UIHostingController”时。 - memical
你能把 catalogView 变成一个属性,然后检查类似于 if navigationController.viewControllers.contains(catalogView) { ... } 的东西吗?(在浏览器中输入,未在 Xcode 中测试)。 - koen
2个回答

11

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

import UIKit
import SwiftUI

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

extension UIViewController {
   var isHosting: Bool { self is AnyUIHostingViewController } 
}

而在你的代码中的某个地方

func someFunction(viewController: UIViewController) {
  if viewController.isHosting {
     print("That is hosting view controller")
  }
}

这绝对应该是一个被接受的答案。谢谢! - undefined

5

由于泛型参数,我们不能将ViewController强制转换为UIHostingController,除非我们知道完整的约束条件。

需要注意的是,这不是一种理想的修复方法,而只是一个权宜之计。

UIHostingControllerUIViewController的子类,因此我们可以这样做。

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

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中有一些ViewControllers,我们可以迭代遍历它们并找到一个ViewController,它的className包含我们正在寻找的内容。

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

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

第一个视图控制器

第二个视图控制器

UIHostingController<Text>

MyHostingController<Text>

然后,您可以使用 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)
}

如我之前所说,这并不是一种理想的解决方案,但在没有更好的方式不知道泛型约束条件的情况下,它可能会有所帮助。


1
这看起来是一个可行的解决方案。谢谢。 - memical

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接