0

I am working on an app that has both swiftUI and storyboard. I have a button in swift UI. On click of this I need to navigate to a storyboard screen. I tried the below code but it is not getting called. Kindly help....

in my swiftUI, the button code is as below,

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

Developer
  • 31
  • 1
  • 8
  • 1
    "I tried the below code but it is not getting called" Firstly, the word below is not an adjective. Secondly, WHAT is not getting called? – El Tomato May 11 '21 at 06:22
  • I believe you should be using `instantiateViewController(withIdentifier:)`, but I'm not sure if that's the problem or not. – West1 May 11 '21 at 06:27

2 Answers2

0

Use NavigationLink with NavigationView

struct MyTestView: View {
    var body: some View {
        NavigationView {
            NavigationLink(
                destination: TestController(),
                label: {
                    Text("Click me").foregroundColor(.white)
                    Image(systemName: "chevron.forward.2").imageScale(.large)}
            )
        }
    }
}
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
  • There may be different ways of doing it. But my requirement was this.... As it is existing code and cannot change the UI elements due to old functionality – Developer May 11 '21 at 16:58
0

I found a solution over here,

How to show NavigationLink as a button in SwiftUI

This works perfectly,

Button(action: {
    print("Floating Button Click")
}, label: {
    NavigationLink(destination: AddItemView()) {
         Text("Open View")
     }
})
Developer
  • 31
  • 1
  • 8