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
You can no longer use NavigationButton. Instead you should use NavigationLink.
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: DetailView()) {
Text("Push new screen")
}
}
}
}
I think this is the easiest and clear way. Use fullScreenCover after UI tool.
Button(action: {
//code
}){
Text("Send")
}.fullScreenCover(isPresented: self.$model.goToOtherView, content: {
OtherView()
})
Here's another way to present a view WITHOUT using NavigationView. This is like UIKit's UIModalPresentationStyle.currentContext
.
struct PresenterButtonView: View {
var body: some View {
PresentationButton(Text("Tap to present"),
destination: Text("Hello world"))
}}
Now we can use NavigationLink
File A:
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Hello World")
NavigationLink(destination: secondView()) {
Text("Hit Me!")
.fontWeight(.semibold)
.font(.title)
.padding()
.foregroundColor(.white)
.background(LinearGradient(gradient: Gradient(colors: [Color(.white),Color(.blue)]), startPoint: .leading, endPoint: .trailing))
.cornerRadius(40)
}
}
}
}
}
File B:
struct secondView: View {
var body: some View {
VStack {
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack(alignment: .top) {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
.padding()
Spacer()
}
}
}
if don't want to show the navigationView you can hide it in destination.
struct ContentViewA : View {
var body: some View {
NavigationView {
VStack {
Text("Hello World")
NavigationLink(destination: ContentViewB()) {
Text("Go To Next Step")
}
}
}
}
}
struct ContentViewB : View {
var body: some View {
NavigationView {
VStack {
Text("Hello World B")
}.navigationBarTitle("")
.navigationBarHidden(true)
}
}
}
or if you want to hide it based on conditions you can use @State
for changing the visibility.
Have to create a DetailView like LandmarkDetail() and call a NavigationButton with destination as LandmarkDetail(). Now detail view was open.
For passing values to detail screen means. by sending like below code.
struct LandmarkList: View {
var body: some View {
NavigationView {
List(landmarkData) { landmark in
NavigationButton(destination: LandmarkDetail()) {
LandmarkRow(landmark: landmark)
}
}
.navigationBarTitle(Text("Landmarks"))
}
}
}