0

I have a screen with textfield and two checkboxes. When user taps in the textfield, keyboard shows up. I want to enable/disable the return key on the soft keyboard based on the following conditions.

Enable when text field is not empty and both the checkboxes are checked. Disable when textfield is empty or either of the checkboxes is unchecked.

Is it possible to achieve this?

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Kone
  • 33
  • 6

3 Answers3

1

create the button array for store the current state of your check box button and connect your checkboxbutton this array, it will be used for get the current state of your button

  var getSelectedState: [UIButton] = []

get your checkbox button current state using first(where:)

Returns the first element of the sequence that satisfies the given predicate.

    var handleButtonState : Bool{
    var setState = false
    if let getNotSelectedState = getSelectedState.first(where: { $0.isSelected == false }) {
        print("get the non selectedState of button \(getNotSelectedState)")
        setState = true
    }
    return setState

}

finally you can use UItextfield delegate for enable/disable the return key using enablesReturnKeyAutomatically

A Boolean value indicating whether the Return key is automatically enabled when the user is entering text.

 // MARK: - Textfield delegates
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
   guard let currentText = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) else { return true }
    textField.enablesReturnKeyAutomatically = !currentText.isEmpty && !handleButtonState ? true :  false        
     return true 

}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

here username and password are two UITextField and right view is the checkbox (Uiview include wrror button or image or checkbox)

self.textField.delegate = self;

   func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        //let maxLength = 4
        let currentString: NSString = textField.text! as NSString
        let newString: NSString =
            currentString.replacingCharacters(in: range, with: string) as NSString

        if newString.length == 1 && textField == self.username {
            self.username.rightView = nil
        }
        if newString.length == 1 && textField == self.password {
            self.password.rightView = nil
        }

        return true//newString.length <= maxLength
    }

I tried it in my project ,I hope it will work for you ...:)

Shivam Parmar
  • 1,520
  • 11
  • 27
0

enabling or disabling of return button automatically happens only on textField's text if it is empty or not, for more customisation we can use textFieldShouldReturn method.

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if let txt = textField.text,
        isBothCheckBoxChecked(),
        !txt.isEmpty{
        return true
    }
    return false
}


func isBothCheckBoxChecked() -> Bool {
    var checked = false

    // write condition here

    return checked
}
Shabbir
  • 289
  • 2
  • 6