Enable button when 2 textifleds are filled

I am trying to not enable a button until both textfields are not nil. Thats it. I tried experimenting with viewdidappear but its not working. As soon as both textfields are not nil the button remains isenable = false.

                 import UIKit

class ViewController: UIViewController {
@IBOutlet var saveButton: UIButton!

@IBOutlet var textB: UITextField!
@IBOutlet var textA: UITextField!
override func viewDidLoad() {

override func viewDidAppear(_ animated: Bool) {

func safeGuard() {
    if textA.text == "" || textB.text == ""{
        saveButton.isEnabled = false
    } else {
        saveButton.isEnabled = true

Hi @timswift

If this is all your code, you call the safeGuard function only once - when a view did appear on the screen.
What you need to do is to call this function every time one of your textfields changed it’s content.
Take a look at this answer on SO - https://stackoverflow.com/questions/7010547/uitextfield-text-change-event

Hope this helps


1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.