-1

I need to enable nextBtn when all textfields has some text only

tried below code: but with this code when i tap on first textfield then my nextBtn becomes enabled why? how to make nextBtn enable after filling the all textfields only. please guide me

override func viewDidLoad() {
    super.viewDidLoad()
    
    nextBtn.isEnabled = false
    nextBtn.alpha = 0.5
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    
    if fullNameTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else if dateOfBirthTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else if schoolNameTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else if schoolLocationTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else if gradeClassTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else{
        nextBtn.alpha = 1
        nextBtn.isEnabled = true
    }
}

edit: i have tried like this. now after taping on second textfield then nextBtn becomes enable why? please guide me

func textFieldDidBeginEditing(_ textField: UITextField) {

    if fullNameTextField.text == "", dateOfBirthTextField.text == "", schoolNameTextField.text == "", gradeClassTextField.text == ""{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else{
        nextBtn.alpha = 1
        nextBtn.isEnabled = true
    }
}
iOS work
  • 33
  • 6
  • For "has some text" the test for equality with nil will not work. A text field's text can be empty without being nil. For example `""` is an empty string but it is not nil. – matt Jun 15 '23 at 11:29
  • @matt, I am so confused, if i check `fullNameTextField.text == ""` like this in same above structure then if i fill all textfields then also `nextBtn ` not changed – iOS work Jun 15 '23 at 11:33
  • @matt, could you guide me to achieve to enable `nextBtn ` when all textfields are filled only – iOS work Jun 15 '23 at 11:35
  • That's what I'm doing. You have to check two things, nil and empty. And you have to check for all of the text fields, not for each of them individually. And didBeginEditing is the wrong place to check. – matt Jun 15 '23 at 11:36
  • `if fullNameTextField.text == "",` should be `if fullNameTextField.text == "" || ` as `,` acts like `&&` and in your case if any not if all – Shehata Gamal Jun 15 '23 at 11:45
  • See https://stackoverflow.com/questions/42861115/why-is-uitextfield-text-an-optional – matt Jun 15 '23 at 11:51

2 Answers2

1

You can simply do it by doing it like this

let fields = [fullNameTextField, dateOfBirthTextField, schoolNameTextField, schoolLocationTextField, gradeClassTextField]
let isVaildFields = fields.allSatisfy { field in
            !field.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
         } 
if isVaildFields {
    nextBtn.alpha = 1
    nextBtn.isEnabled = true
} else {
    nextBtn.alpha = 0.5
    nextBtn.isEnabled = false
}
  • error: Cannot use instance member 'dateOfBirthTextField' within property initializer; property initializers run before 'self' is available for this line 'let fields = [fullNameTextField, dateOfBirthTextField, schoolNameTextField, schoolLocationTextField, gradeClassTextField]' – iOS work Jun 16 '23 at 05:19
  • can u write code for individual textfields bcz at this point i cant create array of textfields – iOS work Jun 16 '23 at 05:21
  • Yeah, sure i'll do it. – Abdullah Nadeem Jun 16 '23 at 11:14
0

One way of doing it is like

class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var fullNameTextField: UITextField!
    @IBOutlet weak var dateOfBirthTextField: UITextField!
    @IBOutlet weak var schoolNameTextField: UITextField!
    @IBOutlet weak var schoolLocationTextField: UITextField!
    @IBOutlet weak var gradeClassTextField: UITextField!
    @IBOutlet weak var nextBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        nextBtn.isEnabled = false
        nextBtn.alpha = 0.5
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        let fields = [fullNameTextField, dateOfBirthTextField, schoolNameTextField,
                      schoolLocationTextField, gradeClassTextField]
        let isValidFields = fields.allSatisfy {
            !($0?.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true)
        }
        
        if isValidFields {
            nextBtn.alpha = 1
            nextBtn.isEnabled = true
        } else {
            nextBtn.alpha = 0.5
            nextBtn.isEnabled = false
        }
    }
}

You can also do it by creating list of text field @IBOutlet, Like following:

class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet var textFields: [UITextField]!
    @IBOutlet weak var nextBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        nextBtn.isEnabled = false
        nextBtn.alpha = 0.5
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        let isValidFields = textFields.allSatisfy {
            !($0.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true)
        }
        
        if isValidFields {
            nextBtn.alpha = 1
            nextBtn.isEnabled = true
        } else {
            nextBtn.alpha = 0.5
            nextBtn.isEnabled = false
        }
    }
}

And if you only want to do it for individual textfields, you can simply do like this.

class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var fullNameTextField: UITextField!
    @IBOutlet weak var dateOfBirthTextField: UITextField!
    @IBOutlet weak var schoolNameTextField: UITextField!
    @IBOutlet weak var schoolLocationTextField: UITextField!
    @IBOutlet weak var gradeClassTextField: UITextField!
    @IBOutlet weak var nextBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        nextBtn.isEnabled = false
        nextBtn.alpha = 0.5
    }
    
    func isValid(_ textField: UITextField) -> Bool {
        return !(textField.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true)
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        if isValid(fullNameTextField), isValid(dateOfBirthTextField),
           isValid(schoolNameTextField), isValid(schoolLocationTextField),
           isValid(gradeClassTextField) {
            nextBtn.alpha = 1
            nextBtn.isEnabled = true
        } else {
            nextBtn.alpha = 0.5
            nextBtn.isEnabled = false
        }
    }
}