how to navigate between views with buttons in swiftUI

9k Views Asked by At

I want to generate the function to my button to be able to make that when I press it it goes to a new view but I do not know how I already have more than 3 hours trying

enter image description here

1

There are 1 best solutions below

0
On

There's a couple problems:

  1. If you want to use programmatic navigation (using a custom button), you'll need an @State to control whether the NavigationLink is active or not.
  2. NavigationLink needs to be somewhere inside a NavigationView.
  3. You also need a VStack, because NavigationView should only wrap around a single View.
struct ContentView: View {
    @State var isPresenting = false /// 1.
    
    var body: some View {
        NavigationView { /// 2.
            VStack { /// 3.
                Button("comenzar") {
                    isPresenting = true
                }
                //        .buttonStyle(fillesRundedCornerButtonStyle())
                
                NavigationLink(destination: HolaView(), isActive: $isPresenting) { EmptyView() }
            }
        }
    }
}

struct HolaView: View {
    var body: some View {
        Text("Hola, como estas?")
    }
}