UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Navigation after action

Forums > SwiftUI

Hello everyone, I am new to swiftui and I am trying to make a login view. Below is what i have right now. As you can see a function is called and the return value is stored in branch. What i want to do is when branch does not equal the string 'false' it should navigate to another view. But when I use a navigationLink it seems to skip over the entire action section. Can you guys help me? It has to navigate to a MenuView with a property: MenuView(title: branch). branch being the return value from the function. Thanks in advance!

TextField("username", text: $username)
    .frame(width: 300, height: 50, alignment: .center)
    .textFieldStyle(RoundedBorderTextFieldStyle())
SecureField("password", text: $password)
    .frame(width: 300, height: 50, alignment: .center)
    .textFieldStyle(RoundedBorderTextFieldStyle())
Button(action: {
    branch = login(username: username, password: password)
    if branch != "false" {
        valid = true
    } else {
        showInvalid = true
        return
    }
}, label: {
    HStack {
        Text("Login")
        Image(systemName: "chevron.right")
    }                    
    .navigationBarHidden(true)
    .frame(width: 300, height: 20)
    .padding(.init(top: 10.0, leading: 0, bottom: 10.0, trailing: 0))
    .overlay(
        RoundedRectangle(cornerRadius: 10)
            .stroke(lineWidth: 2.0)
    )
})
.alert(isPresented: $showInvalid) {
    Alert(title: Text("Invalid"), message: Text("Invalid username or password"), dismissButton: .default(Text("Ok")))
}

2      

Ok I have had a look at this and a couple of points.

  1. Do you have to return a Stringfrom the func login()
  2. I do not see anywhere in your code a NavgationLink?

I have change it about to this

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

    @State private var valid = false
    @State private var showingInvalidAlert = false

    var body: some View {
        NavigationView {
            VStack {
                TextField("username", text: $username)
                    .frame(width: 300, height: 50)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                SecureField("password", text: $password)
                    .frame(width: 300, height: 50)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                Button {
                    valid = login(username: username, password: password)
                    if valid == false {
                        showingInvalidAlert.toggle()
                    }
                } label: {
                    HStack {
                            Text("Login")
                            Image(systemName: "chevron.right")
                        }
                }
                .frame(width: 300, height: 20)
                .padding(.vertical ,10)
                .overlay(
                        RoundedRectangle(cornerRadius: 10)
                            .stroke(lineWidth: 2.0)
                    )

                NavigationLink(destination: Text("Success!"),isActive: $valid) { //<- This will take it to rest
                        EmptyView()
                }

            }
            .navigationBarHidden(true)
            .alert(isPresented: $showingInvalidAlert) {
                Alert(title: Text("Invalid"), message: Text("Invalid username or password"), dismissButton: .default(Text("Ok")))
            }
        }
    }

    func login(username: String, password: String) -> Bool {
        // In here your check to see if username/password are valid
        if username == "danny", password == "password" {
            return true
        }

        return false
    }
}

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.