iOS UITextField-解雇自定义inputView [英] iOS UITextField- dismissing custom inputView
问题描述
我正在为某些文本字段使用自定义inputView,但是当我在文本字段上调用resignFirstResponder时,自定义输入视图不会忽略...任何建议?
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
经过一些研究,这个只有在使用模态视图中显示的viewController时才会出现问题......否则它会正常工作......
After some research, this issue only occurs when working with a viewController thats presented in a modal view...it works ok otherwise...
谢谢
-Daniel
推荐答案
而不是调用 resignFirstResponder
,你应该在视图本身或其任何超级视图上调用 endEditing:
。 endEditing:
将搜索所有具有第一响应者状态的视图的子视图,并要求其辞职。
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:
将布尔标志作为参数。如果设置,它将强制第一响应者辞职;否则,您可以允许它保持焦点(例如,如果输入无效)。对于UITextField,这由委托的 shouldEndEditing:
方法决定。
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)
}
}
这篇关于iOS UITextField-解雇自定义inputView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!