Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UITextField- dismissing custom inputView

I am using a custom inputView for some textfield, however when i call resignFirstResponder on the textfield, the custom input view does not dismiss...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 issue only occurs when working with a viewController thats presented in a modal view...it works ok otherwise...

Thanks

-Daniel

like image 209
Daniel Avatar asked Feb 19 '11 20:02

Daniel


2 Answers

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

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

In general, a good pattern to use is:

- (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)
    }
}
like image 104
benzado Avatar answered Sep 21 '22 18:09

benzado


If you use a modal view, make make sure to enable automatic keyboard dismissal. Add this code to your view controller:

- (BOOL)disablesAutomaticKeyboardDismissal
{
    return NO;
}
like image 41
freytag Avatar answered Sep 23 '22 18:09

freytag