iOS UITextField-解雇自定义inputView

Dan*_*iel 7 uitextfield ios inputview

我正在为某些文本字段使用自定义inputView,但是当我在文本字段上调用resignFirstResponder时,自定义输入视图不会忽略...任何建议?

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
Run Code Online (Sandbox Code Playgroud)

经过一些研究,只有在使用模态视图中呈现的viewController时才会出现此问题...否则正常工作...

谢谢

-Daniel

ben*_*ado 7

resignFirstResponder您应该调用endEditing:视图本身或其任何超级视图,而不是调用.endEditing:将在所有子视图中搜索具有第一响应者状态的任何视图,并要求其辞职.

endEditing:将布尔标志作为参数.如果设置,它将强制第一响应者辞职; 否则,您可以允许它保持焦点(例如,如果输入无效).对于UITextField,这由委托的shouldEndEditing:方法决定.

一般来说,一个好的模式是:

- (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)
    }
}
Run Code Online (Sandbox Code Playgroud)