我有两个文本字段。点击后,第一个显示一个QWERTY键盘,第二个显示选择器。 我的问题是当我点击我的第一个textField时,键盘正常显示。现在,在我输入任何东西之后,我直接想要点击第二个textField,这是显示选择器,键盘应该消失,但键盘仍然存在。 我想单击第二个textField时立即隐藏键盘。键盘不隐藏时点击第二个文本字段
回答
有两种方式来实现这一点 -
第一:使用UITextFieldDelegate。 对于觉得─ 列出UITextFieldDelegate在您的视图控制器的协议列表一样,
@interface ViewController : UIViewController <UITextFieldDelegate>
然后让你的ViewController符合文本字段的委托,以实现像textFieldDidBeginEditing
的方法,并textFieldDidEndEditing:
所以,去到viewDidLoad
方法和符合的UITextField的协议喜欢 -
- (void)viewDidLoad
{
[super viewDidLoad];
self.firstTextField.delegate = self; //assuming your first textfield's
//name is firstTextField
//Also give your first textfield a tag to identify later
self.firstTextField.tag = 1;
}
现在,你都设置为实现委托方法。但是,要首先实现目标,需要使用UITextField实例来了解何时输入firstTextField。因此,声明UITextField
类型的属性。这样做,在接口文件喜欢 -
@property(nonatomic, strong) UITextField *currentTextField;
现在在textFieldDidBeginEditing
委托方法中,firstTextField实例分配到currentTextField当您开始键入它喜欢 -
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField.tag == 1){
self.currentTextField = textField;
}
}
后,在textFieldDidEndEditing
方法,检查它是否是当前的文本框要从中走出来,并取消键盘喜欢 -
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag == 1){
[self.currentTextField resignFirstResponder];
}
return YES;
}
秒:您可以使用UIResponder。作为ViewControllers从UIResponder继承,你只需重写方法 - touchesBegan:withEvent
方法,一些喜欢 -
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];// this will do the trick
}
在这种情况下,当你点击了侧的文本字段,键盘会自动消失。
使用UITextFieldDelegate
组代表在ViewDidLoad
self
,然后把这个代码
func textFieldDidEndEditing(textField: UITextField)
{
yourtextfieldname.resignFirstResponder()
return true
}
我正在这样做,但它不工作 –
Step 1 :- Code for ViewController.h :-
//add UITextFieldDelegate just after UIViewController
@interface ViewController : UIViewController<UITextFieldDelegate>
Step 2 :- Code For ViewController.m :-
//inside viewdidLoad write following code, where txtInput is outlet for input text field
_txtInput.delegate = self;
// Last Thing write following code just above @end
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
//that's it use this very simple way
- 1. 的XCode文本字段隐藏键盘
- 2. 点击UIWebView隐藏键盘
- 3. 隐藏第二次点击
- 4. 隐藏键盘时点击Ext.Form.Select
- 5. 虚拟键盘隐藏字段/文本域/ CONTENTEDITABLE(下面隐藏键盘)
- 6. 点击一个UIScrollView隐藏键盘?
- 7. 在点击编辑文本字段第二次显示的默认键盘
- 8. 如何知道键盘何时隐藏文本字段?
- 9. 测试键盘在显示时是否隐藏文本字段
- 10. 当按钮点击时隐藏键盘(片段)
- 11. 为什么文本字段不能响应隐藏键盘?
- 12. 移动视图,以便键盘不隐藏文本字段
- 13. 键盘隐藏文本框
- 14. UIWebView在第二次加载时关注文本字段时不显示键盘
- 15. Android中WebView中的文本字段被键盘隐藏
- 16. Android隐藏键盘密码字段
- 17. iPhone键盘在UIWebView中隐藏字段
- 18. 隐藏键盘密码字段Android
- 19. 键盘隐藏输入字段的WebView
- 20. 隐藏文本被点击
- 21. UITextField失去焦点时隐藏键盘
- 22. 带文本字段的UIAlertController - 点击返回按钮只隐藏键盘,不执行操作?
- 23. 如何使用背景隐藏数字键盘点击?
- 24. 只有在ipad上点击某个文本字段时才显示键盘?
- 25. 点击ListView中的复选框时隐藏键盘
- 26. iPhone:点击UITextField时是否可以隐藏键盘?
- 27. 如何在editText外部点击时隐藏键盘?
- 28. 当完成点击时,软键盘未隐藏AutocompleteTextView
- 29. 隐藏键盘按钮被点击时xamarin形式
- 30. 隐藏键盘
第一点击后第二文本字段被做self.view.endEditing则目前选择器 –
我没有...但不工作 –
..删除textfild&使用按钮为什么使用textfield –