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

A textfield two states and a button

Forums > SwiftUI

My problem is as follows: My view has a textfield and a disabled button along with two states. One state for the value in the textfield and one state for the disabled button. I want to enable the button when something (at least 1 caracter) is entered in the textfield, and redisables it if the user deletes the content. I have the idea working,sorts of. I capture the initial hit on the field, and the result on the commit(return) on the field. But I cannot find how to know if the field is empty or not. Any help will be most welcome PS I know that there is a Combine way as presented in one of the WWDC 19 video, but I would like to have it work differently. Best regards Lars import SwiftUI

struct Avirer: View { @State var isDisabled: Bool = true @State var lab: String = ""

func editChange()  {
//this function is activated when the field is touched         self.isDisabled.toggle()
}

var body: some View {
    VStack {
        HStack{
            Text("Enter Key  :")

            TextField("Placeholder", text: $lab, onEditingChanged:{ _ in self.editChange()},
                      onCommit: {EmptyView()})
        }
        Button(action: {self.isDisabled.toggle() }) {
            Text("Activate the submit button")
        }

// Just an indicator that the state variable works correctly Text(lab)

        Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/) {
            Text("Submit")
        }.disabled(isDisabled)
    }.padding(20)

    }
}

struct Avirer_Previews: PreviewProvider { static var previews: some View { Avirer() } }

2      

Have you tried

var isDisabled: Bool {
        if lab.isEmpty {
            return true
        }
        return false
    }

more information at https://www.hackingwithswift.com/books/ios-swiftui/checking-for-a-valid-address

2      

It sort of works but, lab.isEmpty will remain true until you hit enter. So even if you have entered " caracters isEmpty will answer true. This is strange as it is transferred caracter by caracter to the other field so there must be a way to capture the information. I would like to count the caracters et enable the button only when the first caracter is entered, and disable it again if the user.

2      

Sorry. Are you asking that as soon as you type a character in the textfield then the button is active

then put

.disabled(lab == "")

as soon as you type a character then the button will be enabled

3      

Exactly what I was looking for. Absolutely super.

Thank you so much

2      

Thank you @NigelGee for your help. I was looking to do the same thing with a textfield. I didn't even know about the .disabled modifier. Totally solved my issue.

Mark

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.