Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login Screen SwiftUI

Tags:

swift

swiftui

I have TextFields for Login and SecureField for Password. How can I go to another view when my Login and Password are correct?

struct LoginBoard: View {
@State private var login = "Tony"
@State private var password = "1234"

var body: some View {
    
    ZStack { 
        VStack {
            HStack {
                Text("Enter Login and Password")
            }

            HStack {
                Image(systemName: "person")
                TextField("Login", text: $login)
            }
            
            HStack {
                SecureField("Password", text: $password)
            }
            
            Button("Login") {
                   
            }
         }
      }
   }
}
like image 841
AntonyCatcher Avatar asked Sep 11 '25 12:09

AntonyCatcher


2 Answers

Wrap your Stacks inside a NavigationView{} and use a NavigationLink{} to direct to another view. Sample code is below:

import SwiftUI

struct LoginBoard: View { 

    @State private var login = "Tony"
    @State private var password = "1234"

    var body: some View {
        NavigationView {
            ZStack {
                VStack {
                    HStack {
                        Text("Enter Login and Password")
                    }
                    HStack {
                        Image(systemName: "person")
                        TextField("Login", text: $login)
                    }              
                    HStack {
                        SecureField("Password", text: $password)
                    }
                    NavigationLink {
                        WelcomeView()
                    } label: {
                        Text("Login")
                            .foregroundColor(.blue)
                    }
                    .disabled((login == "Tony" && 
                               password == "1234") ? false : true)
                }
            }
        }
    }
}

struct WelcomeView: View {
    var body: some View {
        Text("welcome!")
    }
}
like image 54
Steven-Carrot Avatar answered Sep 14 '25 18:09

Steven-Carrot


You should use NavigationView that's an equivalent to navigation controller in UIKit and use NavigationLink as the segue or trigger for navigation.

struct LoginBoard: View {
 
    @State private var login = "Tony"
    @State private var password = "1234"
    @State isLoginSuccess = false

    var body: some View {
    // like navigation controller 
    // that handles the navigation of views
 
        NavigationView { 
        // DestinationView is the view will go 
        // to if credentials is correct
            NavigationLink(destination: DestinationView(), 
                              isActive: $isLoginSuccess) { }
            ZStack { 
                VStack {
                    HStack {
                        Text("Enter Login and Password")
                    }
                    HStack {
                        Image(systemName: "person")
                        TextField("Login", text: $login)
                    }            
                    HStack {
                        SecureField("Password", text: $password)
                    }
                    Button("Login") {
                    // if user and password are correct change          
                    // isLoginSuccess to true and will navigate 
                    // to the next View 
                        isLoginSuccess = true
                    }
                }
            }
        }
    }
}
like image 37
belal medhat Avatar answered Sep 14 '25 16:09

belal medhat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!