简体   繁体   English

如何在 swift UI 中单击按钮时从 swift UI 导航到 storyboard?

[英]How to navigate from swift UI to storyboard on button click in swift UI?

I am working on an app that has both swiftUI and storyboard.我正在开发一个同时具有 swiftUI 和 storyboard 的应用程序。 I have a button in swift UI.我在 swift UI 中有一个按钮。 On click of this I need to navigate to a storyboard screen.单击此按钮后,我需要导航到 storyboard 屏幕。 I tried the below code but it is not getting called.我尝试了下面的代码,但没有被调用。 Kindly help....请帮助....

in my swiftUI, the button code is as below,在我的 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) {
       
    }
}

Please help me...请帮我...

Use NavigationLink with NavigationViewNavigationLinkNavigationView一起使用

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

I found a solution over here,我在这里找到了解决方案,

How to show NavigationLink as a button in SwiftUI 如何在 SwiftUI 中将 NavigationLink 显示为按钮

This works perfectly,这完美地工作,

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

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

相关问题 如何在从 Swift UI 框架到达的 storyboard 中单击按钮进行导航? - How to navigate on click of button from storyboard that was reached from Swift UI framework? 如何通过单击按钮在 Swift UI 中从一个视图导航到另一个视图 - How to navigate from one view to another in Swift UI on a click of button 如何在Swift Playground中从加载的故事板视图访问UI对象? - How to access UI objects from loaded storyboard view in swift playground? 如何在快速单击按钮时从故事板调用xib文件 - how to call xib file from storyboard on button click in swift 在下一个“故事板”中按下按钮,在Swift中以编程方式设计UI - Button pressed to next “storyboard” Programmatically designing UI in Swift 如何在XCode UI中将情节提要线连接到Swift代码? - How do I wire up the storyboard to Swift code in XCode UI? Swift pagecontroller故事板UI对象与UIPageviewController连接 - Swift pagecontroller storyboard UI object connect with UIPageviewcontroller 在没有情节提要Swift 4的情况下构建UI元素 - Building UI Elements without the storyboard Swift 4 如何快速单击弹出式ViewController上的按钮单击导航到TabBar ViewController? - How to navigate to TabBar ViewController on click of button on popover ViewController in swift? 将按钮添加到导航栏标题 Swift ui - Add button to navigationBarTitle Swift ui
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM