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?
UITextFie开发者_C百科ld *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
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)
}
}
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;
}
精彩评论