I have two view one is LoginView. Another is WelcomeView. I want to go from LoginView to WelcomeView onclick button from LoginView.
Here is my code..
LoginView
struct LoginView: View {
var body: some View {
VStack{
Button(action: {
print("*** Press go login view ****")
}) {
Text("Login")
.font(.custom(TextConstant.keyValues.front_name, size: 30))
.foregroundColor(.white)
.fontWeight(.bold)
.frame(minWidth: 0, maxWidth: .infinity)
.padding(.all,20)
.foregroundColor(.blue)
.background(LinearGradient(gradient: Gradient(colors: [.green, .green]), startPoint: .leading, endPoint: .trailing))
.cornerRadius(10)
}
}
}
}
Here is WelcomeView
struct WelcomeView: View {
var body: some View {
Text("Hello ")
}
}
I want to go another page on clcik button & back to previous onclick button. Please help.
you could try this to go from LoginView
to WelcomeView
on your button click in LoginView
:
struct LoginView: View {
@State private var showWelcomeView = false
var body: some View {
NavigationView {
VStack {
Button(action: { showWelcomeView = true }) {
Text("Login")
.font(.custom(TextConstant.keyValues.front_name, size: 30))
.foregroundColor(.white)
.fontWeight(.bold)
.frame(minWidth: 0, maxWidth: .infinity)
.padding(.all,20)
.foregroundColor(.blue)
.background(LinearGradient(gradient: Gradient(colors: [.green, .green]), startPoint: .leading, endPoint: .trailing))
.cornerRadius(10)
}
NavigationLink("", destination: WelcomeView(), isActive: $showWelcomeView)
}
}
}
}
EDIT-1:
NavigationView
iOS 13.0–17.2 Deprecated.
Use NavigationStack
and .navigationDestination(...)
, such as:
struct LoginView: View {
@State private var showWelcomeView = false
var body: some View {
NavigationStack {
VStack {
Button(action: { showWelcomeView = true }) {
Text("Login")
.foregroundColor(.white)
.fontWeight(.bold)
.frame(minWidth: 0, maxWidth: .infinity)
.padding(.all,20)
.foregroundColor(.blue)
.background(LinearGradient(gradient: Gradient(colors: [.green, .green]), startPoint: .leading, endPoint: .trailing))
.cornerRadius(10)
}
}
.navigationDestination(isPresented: $showWelcomeView) {
WelcomeView()
}
}
}
}