0
votes

I have my code in Swift 3. I have two UITextFields and a UIButton in my view. I have the textFieldShouldEndEditing delegate of the first UITextField which is fired when I focus on the 2nd UITextField. However when I tap on the UIButton this delegate is not fired. I had thought that when a UIButton gets focus then the UITextField’s event will automatically fire since it is losing focus. However, when I tap on the UIButton, the cursor is still blinking in the UITextField, which means it is not losing focus.

Any ideas on why the UIButton is not getting focus will be most appreciated.

Thanks.

1

1 Answers

0
votes

This is an infuriating aspect of developing forms on iOS. Button taps don't remove focus from UITextField / UITextView the same way they would in an HTML form.

I'm sure there are more elegant ways to solve this, but if I want to consistently do this across all forms I ensure that the following line of code is run when users tap on any buttons of mine.

- (void)userDidTapButton:(id)sender 
{
    [[[UITextField new] becomeFirstResponder] resignFirstResponder]
    // the above embarrassing line of code basically creates a new textfield
    // puts focus in it (thereby removing focus from any existing textfields
    // and then removes focus again
    // this ensures that delegate events fire on your existing textfields

// finally, run any other button code
}