Ask Your Question

Revision history [back]

You can achieve this by using the @State property wrapper and the NavigationLink view. Here's an example code:

import SwiftUI

struct ContentView: View {
    @State private var username = ""
    @State private var password = ""
    @State private var isLoggedIn = false

    var body: some View {
        NavigationView {
            VStack {
                TextField("Username", text: $username)
                    .textFieldStyle(RoundedBorderTextFieldStyle())

                SecureField("Password", text: $password)
                    .textFieldStyle(RoundedBorderTextFieldStyle())

                Button(action: login) {
                    Text("Login")
                        .padding()
                        .foregroundColor(.white)
                        .background(Color.blue)
                        .cornerRadius(10)
                }
            }
            .padding()
            .navigationTitle("Login")
            .navigationBarTitleDisplayMode(.inline)
            .sheet(isPresented: $isLoggedIn) {
                Text("Logged in!").padding()
            }
        }
    }

    func login() {
        // Make HTTP request to login API
        // If successful, set isLoggedIn to true and navigate to new view
        isLoggedIn = true
    }
}

In this code, the @State property wrapper is used to keep track of the username, password, and login status. The NavigationLink view is used to navigate to a new view upon successful login.

When the user taps the "Login" button, the login function is called. Inside this function, you can make an HTTP request to a login API using the username and password values. If the login is successful, you can set isLoggedIn to true.

The new view is displayed using the sheet modifier. By setting the isPresented parameter to true, the new view is displayed.