我有一个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
。 - ahezecatalogView
变成一个属性,然后检查类似于if navigationController.viewControllers.contains(catalogView) { ... }
的东西吗?(在浏览器中输入,未在 Xcode 中测试)。 - koen