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
1 Answer
There's a couple problems:
- If you want to use programmatic navigation (using a custom button), you'll need an
@Stateto control whether theNavigationLinkis active or not. NavigationLinkneeds to be somewhere inside aNavigationView.- You also need a
VStack, becauseNavigationViewshould 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?")
}
}
