简体   繁体   中英

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?

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.

Essentially: SwiftUI TabView -> UIKit TableView -> SwiftUI View

Or will I need to setup storyboard to achieve this segue?

Create a hidden Navigation Link with the 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:

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. 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 ?

TabView => NavigationView => List => SwiftUIView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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