简体   繁体   English

以编程方式转到 SwiftUI 视图

[英]Programmatically segue to SwiftUI view

In the Integrating SwiftUI WWDC video it shows the example of using an IBSegueAction to transition to a SwiftUI view, is it possible to do this programmatically?集成 SwiftUI WWDC 视频中,它显示了使用 IBSegueAction 转换到 SwiftUI 视图的示例,是否可以通过编程方式执行此操作?

I have a SwiftUI app with a UIKit TableViewController (via UIViewControllerRepresentable), it's this table I'm looking to segue from to another SwiftUI view.我有一个 SwiftUI 应用程序和一个 UIKit TableViewController(通过 UIViewControllerRepresentable),这是我希望从另一个 SwiftUI 视图中切换的表。

Essentially: SwiftUI TabView -> UIKit TableView -> SwiftUI View本质上:SwiftUI TabView -> UIKit TableView -> SwiftUI View

Or will I need to setup storyboard to achieve this segue?还是我需要设置 storyboard 来实现这个转场?

Create a hidden Navigation Link with the Bool.使用 Bool 创建一个隐藏的导航链接。

NavigationLink(destination: NextView(), isActive: self.isShowNext) {
    Text("")
}.hidden()

It's theoretically possible in your Coordinator for your UIViewRepresentable to push a UIHostingController with your SwiftUI view if it's navigationController property is set:如果设置了navigationController属性,理论上可以在您的 UIViewRepresentable 的Coordinator中使用您的UIViewRepresentable视图推送UIHostingController

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    let view = SomeView()
    navigationController?.pushViewController(UIHostingController(root: view))
}

But I think you will find it easiest to use UIKit up until the point where you don't think you need it again.但我认为您会发现最容易使用UIKit直到您认为不再需要它为止。 So instead of:所以而不是:

TabView => NavigationView => wrapped UITableViewController => some SwiftUI View

it should be:它应该是:

UITabBarController => UINavigationController => UITableViewController => UIHostController(root: SwiftUIView)

But why don't you simply want to use List ?但是你为什么不简单地使用List呢?

TabView => NavigationView => List => SwiftUIView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM