Enable a button in Swift only if all text fields have been filled out

前端 未结 10 612
误落风尘
误落风尘 2020-12-01 01:20

I am having trouble figuring out how to change my code to make it so the Done button in the navigation bar is enabled when my three text fields are filled out.

I cur

相关标签:
10条回答
  • 2020-12-01 01:35

    This works for me: Hope it helps

      func textFieldDidEndEditing(textField: UITextField) {
            if txtField1.hasText() && textField2.hasText() && textField3.hasText(){
                doneBarButton.enabled = true
            }
        }
    
    0 讨论(0)
  • 2020-12-01 01:40

    Swift 5.1 /Xcode 11

    override func viewDidLoad() {
        super.viewDidLoad()
        setupAddTargetIsNotEmptyTextFields()        
    }
    
    func setupAddTargetIsNotEmptyTextFields() {
        okButton.isHidden = true //hidden okButton
        nameUserTextField.addTarget(self, action: #selector(textFieldsIsNotEmpty), 
                                    for: .editingChanged)
        emailUserTextField.addTarget(self, action: #selector(textFieldsIsNotEmpty), 
                                    for: .editingChanged)
        passwordUserTextField.addTarget(self, action: #selector(textFieldsIsNotEmpty), 
                                    for: .editingChanged)
        confimPasswordUserTextField.addTarget(self, action: #selector(textFieldsIsNotEmpty), 
                                    for: .editingChanged)        
       }
    

    and then create the selector method and use guard:

    @objc func textFieldsIsNotEmpty(sender: UITextField) {
    
        sender.text = sender.text?.trimmingCharacters(in: .whitespaces)
    
        guard
          let name = nameUserTextField.text, !name.isEmpty,
          let email = emailUserTextField.text, !email.isEmpty,
          let password = passwordUserTextField.text, !password.isEmpty,
          let confirmPassword = confimPasswordUserTextField.text,
              password == confirmPassword          
          else
        {
          self.okButton.isHidden = true
          return
        }
        // enable okButton if all conditions are met
        okButton.isHidden = false
       }
    
    0 讨论(0)
  • 2020-12-01 01:40

    why not move the checking functionality to a separate function

    func setDoneButtonStatus()
    {
        let habitNameText: NSString = (habitNameField.text!).stringByReplacingCharactersInRange(range, withString: string)
        let goalText: NSString = (goalField.text!).stringByReplacingCharactersInRange(range, withString: string)
        let frequencyText: NSString = (frequencyField.text!).stringByReplacingCharactersInRange(range, withString: string)
    
        doneBarButton.enabled = (habitNameText.length != 0) && (goalText.length != 0) && (frequencyText.length != 0)
    }
    

    and then use

    func textFieldShouldReturn(textField: UITextField) -> Bool
    {
        textField.resignFirstResponder()
        setDoneButtonStatus()
    }
    
    0 讨论(0)
  • 2020-12-01 01:50

    The best way would be to Add observer in ViewDidLoad method. Than just check in textField Delegate method whether all TextFields are filled up or not. Once its filled up call oberserver method & in that you just need to enable button.

    Note:

    • You can use observer for both enable or disable button

    Hope it will help you.

    0 讨论(0)
提交回复
热议问题