iOS UITextField- dismissing custom inputView

折月煮酒 提交于 2019-12-21 12:17:22

问题


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


回答1:


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)
    }
}



回答2:


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;
}


来源:https://stackoverflow.com/questions/5053465/ios-uitextfield-dismissing-custom-inputview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!