【问题标题】:How to navigate from swift UI to storyboard on button click in swift UI?如何在 swift UI 中单击按钮时从 swift UI 导航到情节提要?
【发布时间】:2021-08-01 12:12:51
【问题描述】:

我正在开发一个同时具有 swiftUI 和情节提要的应用程序。我在 swift UI 中有一个按钮。单击此按钮后,我需要导航到情节提要屏幕。我尝试了下面的代码,但没有被调用。请帮忙....

在我的swiftUI中,按钮代码如下,

    Button(action:{ TestController()
                                    
                                }, label:
                                    {
                                        Text("Click me").foregroundColor(.white)
                                        Image(systemName: "chevron.forward.2").imageScale(.large)})



struct TestController: UIViewControllerRepresentable {
    
    func makeUIViewController(context: Context) -> some UIViewController {
        let storyboard = UIStoryboard(name: "test", bundle: Bundle.main)
        let controller = storyboard.instantiateViewController(identifier: "testView")
        return controller
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
       
    }
}

请帮帮我...

【问题讨论】:

  • "我尝试了下面的代码,但它没有被调用" 首先,下面的词不是形容词。其次,什么没有被调用?
  • 我相信你应该使用instantiateViewController(withIdentifier:),但我不确定这是否是问题。

标签: ios swift swiftui navigation storyboard


【解决方案1】:

NavigationLinkNavigationView 一起使用

struct MyTestView: View {
    var body: some View {
        NavigationView {
            NavigationLink(
                destination: TestController(),
                label: {
                    Text("Click me").foregroundColor(.white)
                    Image(systemName: "chevron.forward.2").imageScale(.large)}
            )
        }
    }
}

【讨论】:

  • 可能有不同的方法。但我的要求是这样......因为它是现有代码并且由于旧功能而无法更改 UI 元素
【解决方案2】:

我在这里找到了解决方案,

How to show NavigationLink as a button in SwiftUI

这很好用,

Button(action: {
    print("Floating Button Click")
}, label: {
    NavigationLink(destination: AddItemView()) {
         Text("Open View")
     }
})

【讨论】:

  • 为什么不使用直接导航线????不需要按钮。
猜你喜欢
  • 2021-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多