IOS UITextField - reject user inputView

I use a custom inputView for some text field, however, when I call resignFirstResponder in the text field, the custom input view does not reject ... any suggestions?

UITextField *f=[[UITextfield alloc] init]; UIView *view=[[UIView alloc] initWithFrame..]; [f setInputView:view]; //random example of how to set a textfields input view to a custom view 

After some research, this problem only occurs when working with the viewController presented in modal form ... it works fine otherwise ...

thanks

-Daniel

+6
ios uitextfield
source share
2 answers

Instead of calling resignFirstResponder you should call endEditing: in the view itself or in any of its superviews. endEditing: will search for all subqueries for any view that has the status of the first responder and ask him to resign.

endEditing: accepts a boolean flag as a parameter. If set, this will force the first responder to resign; otherwise, you can let it maintain focus (if the input is invalid, for example). For UITextField, this is determined by the delegate method shouldEndEditing:

In general, a good template to use:

 - (void)saveAction:(id)sender { if ([self.view endEditing:NO]) { NSString *text = self.textField.text; // save text, login, do whatever } else { // show an alert (or rely on whatever refused to resign to inform the user why) } } 
+6
source share

If you use a modal view, make sure that the keyboard is automatically turned off. Add this code to your controller:

 - (BOOL)disablesAutomaticKeyboardDismissal { return NO; } 
+4
source share

All Articles