I\'ve got a basic view with a button using SwiftUI and I\'m trying to present a new screen/view when the button is tapped. How do I do this? Am I suppose to create a delegat
I think Jake's answer is the basic way to NextView.
And I think the way bellow is a simple, formal, and dynamic way to try, if you really need to hit a BUTTON. According to Paul Hudson's Video, from 10'00" to 12'00".
(should go to 12'00" to 15'00", if you want to go to different views by tapping different buttons.) (should go to 15'00" to 16'00", if you want to go to second view and go back automatically.) And more
And here is the code example.
import SwiftUI
struct ContentView: View {
@State var areYouGoingToSecondView: Bool // Step 2
var body: some View {
NavigationView{ // Step 1
VStack {
// Step 3
NavigationLink(destination: YourSecondView(), isActive: $areYouGoingToSecondView) { EmptyView() }
Text("Hello World")
Button(action: {
self.areYouGoingToSecondView = true // Step 4
}) {
Text("Do Something (Go To Second View)")
.font(.largeTitle)
.fontWeight(.ultraLight)
}
}
}
}
}