如何从SwiftUI视图导航到Storyboard VC?

3

我创建了这个SwiftUI视图:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView{
            MasterView()
        }.navigationViewStyle(DoubleColumnNavigationViewStyle())
    }
}

struct MasterView: View {
     var body: some View {
            Form {
                Section(header: Text("Geplant")) {
                    Section {
                        NavigationLink(destination: /* How can I navigate to a Storyboard ViewController?*/) { Text("Berlin") }

                    }
                
                }
            }
            .navigationBarTitle("Wähle Reise")
    }
}

我想在“NavigationLink(destination:...)”中导航到一个Storyboard视图控制器。

之前,我使用UIHostingController从UIKit视图控制器导航到了SwiftUI ContentView。

有人可以帮忙吗?

如需其他代码/截图,请随时与我联系:)

1个回答

9

使用 UIViewControllerRepresentable

struct UIKitView: UIViewControllerRepresentable {
    typealias UIViewControllerType = UIKitViewController

    func makeUIViewController(context: Context) -> UIKitViewController {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let viewController = sb.instantiateViewController(identifier: "UIKitViewController") as! UIKitViewController
        return viewController
    }
    
    func updateUIViewController(_ uiViewController: UIKitViewController, context: Context) {
        
    }
}

现在可以使用

NavigationLink(destination: UIKitView()) { Text("Berlin") }

太棒了!我刚刚将它复制到我的代码下面,但是它显示“类型'UIKitView'不符合协议'UIViewControllerRepresentable'”,“无法在范围内找到类型'UIKitViewController'”,我需要导入什么? - MunichCoder
我需要用什么替换UIKitViewController吗? - MunichCoder
UIKitViewController 这个是我的演示版本。你需要用你的控制器名称替换它。 - Raja Kishan
当我在SwiftUI和基于Nib的Objective-C编写的ViewController之间进行接口时,这对我非常有效。 - Silicon Fox

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