SwiftUI open another view on button click

Am trying to navigate to another view page. I tried many methods but none worked. If there is a way to navigate to another page without navigationtool... then it will be much help ful

    struct ContentView: View {

    @State var fname: String = ""
    @State var lname: String = ""
    @State var num: String = ""
    @State var pass: String = ""
    @State private var registerionAlert = false
    @State private var registerionAlertActive: ActiveAlert = .success

    var body: some View {
        VStack(alignment: .center) {
            Form {
                Text("Registeration Form")
                    .font(.title)
                    .fontWeight(.bold)
                    .foregroundColor(Color.blue)
                    .multilineTextAlignment(.center)
                    .padding(.leading, 25.0)
                TextField("Enter your first name...", text: $fname)
                    .padding()
                    .border(/*@START_MENU_TOKEN@*/Color.blue/*@END_MENU_TOKEN@*/, width: /*@START_MENU_TOKEN@*/1/*@END_MENU_TOKEN@*/)
                TextField("Enter your last name...", text: $lname)
                    .padding()
                    .border(/*@START_MENU_TOKEN@*/Color.blue/*@END_MENU_TOKEN@*/, width: /*@START_MENU_TOKEN@*/1/*@END_MENU_TOKEN@*/)
                TextField("Enter your phone number...", text: $num)
                    .padding()
                    .border(/*@START_MENU_TOKEN@*/Color.blue/*@END_MENU_TOKEN@*/, width: /*@START_MENU_TOKEN@*/1/*@END_MENU_TOKEN@*/)
                TextField("Enter a password...", text: $pass)
                    .padding()
                    .border(/*@START_MENU_TOKEN@*/Color.blue/*@END_MENU_TOKEN@*/, width: /*@START_MENU_TOKEN@*/1/*@END_MENU_TOKEN@*/)
                Button(action: {
                    self.registerTapped()
                }) {
                    Text("Register")
                        .multilineTextAlignment(.center)
                }
                .padding(0.0)
                .frame(width: 150.0, height: 30.0)
                .background(/*@START_MENU_TOKEN@*/Color.green/*@END_MENU_TOKEN@*/)
                .accentColor(/*@START_MENU_TOKEN@*/.white/*@END_MENU_TOKEN@*/)
                .cornerRadius(/*@START_MENU_TOKEN@*/5.0/*@END_MENU_TOKEN@*/)
                .offset(x: 95.0, y: /*@START_MENU_TOKEN@*/0.0/*@END_MENU_TOKEN@*/)
                .alert(isPresented: $registerionAlert){
                    switch registerionAlertActive {
                    case .success:
                        return Alert(
                            title: Text("Register"),
                            message: Text("Registered Successfully"),
                            dismissButton: .default(
                                Text("Got it!"),
                                action: {
                                    loginView()
                                    print("success")
                                }
                            )
                        )
                    case .failed:
                        return Alert(
                            title: Text("Register"),
                            message: Text("Registered Unsuccessful, Try again later."),
                            dismissButton: .default(
                                Text("Got it!"))
                        )
                    }
                }

            }
            .padding(10.0)
            .background(/*@START_MENU_TOKEN@*/Color.blue/*@END_MENU_TOKEN@*/)
        }
        .padding()
        .background(/*@START_MENU_TOKEN@*/Color.orange/*@END_MENU_TOKEN@*/)
    }

    func registerTapped() {

        let params: [String: Any] = [
            "first_name": "\(fname)",
            "last_name": "\(lname)",
            "phone": "\(num)",
            "password": "\(pass)"
        ]

        AF.request(
            "http://192.168.0.9/mobile-api/public/register",
            method: .post,
            parameters: params,
            encoding: JSONEncoding.default
        ).validate().responseString() {
            response in
            switch response.result {
            case .success(_):
                self.registerionAlertActive = .success
                break
            case .failure(_):
                self.registerionAlertActive = .failed
                break
            }
            self.registerionAlert = true
        }
    }

}

my another page is given below in which i havent add much of thigs jz only sample views

//
//  loginView.swift
//  test
//
//  Created by Leo Thomas on 18/01/20.
//  Copyright © 2020 Leo Thomas. All rights reserved.
//

import SwiftUI

struct loginView: View {
    var body: some View {
        Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
    }
}

struct loginView_Previews: PreviewProvider {
    static var previews: some View {
        loginView()
    }
}

please help me navigate to another page

1 answer

  • answered 2020-01-18 12:22 Ruchi Makadia

    you can do this

    struct ContentView: View {
    @State var isOpen: Bool = false
    var body: some View {
        ZStack {
            VStack {
                Button(action: {
                    self.isOpen = true
                }, label: {
                    Text("Tap me")
                        .foregroundColor(obj_saved_color.set_color)
                }).sheet(isPresented: $isOpen, content: {
                   SecondView()
                })
            }
        }
    }}
    
    struct SecondView: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    var body: some View {
        VStack {
            Text("Second View")
        }
    }}