UITapGestureRecognizer 并使键盘在点击背景时消失
我创建了一个表单,当输入您的年龄等数据时,会出现键盘(仅限数字)。
我希望当用户点击背景时键盘消失,并且我想在 7 下方的空槽中(零旁边)添加一个“完成”按钮。 (我使用数字键盘)
我找到了这个例子,但我有几个问题。
如果
-(void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard
{
[aTextField resignFirstResponder];
[aTextField1 resignFirstResponder];
[aTextField2 resignFirstResponder];
[aTextField3 resignFirstResponder];
}
我的表单中有超过 1 个文本字段。 我需要在missKeyboard 方法中写入每个文本字段吗?
I created a form and the keypad (Numeric only) appears when entering data like your age.
I want the keyboard to disappear when the user taps the background and I want to add a "Done" button in the empty slot under the 7 (next to the zero). (im using the Number Pad keyboard)
I found this example but I have a few questions.
In
-(void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard
{
[aTextField resignFirstResponder];
[aTextField1 resignFirstResponder];
[aTextField2 resignFirstResponder];
[aTextField3 resignFirstResponder];
}
If I have more than 1 text field in my form.
Will I need to write every textfield in the dismissKeyboard method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单的方法是使用 UIView 中提供的方法
所以只需这样做:
它将支持您在页面上添加的任何更多文本字段(当然在 UIView 下)
Easy way to do this is to use the method provided in UIView
So just do this:
and it will support any more text fields you add on your page (under that UIView of course)
您应该只将
dismissKeyboard
发送到您当前正在编辑的textField
。在您的代码中,您遇到了内存泄漏。更好地使用这个:
要检查
UITextField
当前是否处于编辑模式
,您可以检查其属性:一个布尔值,指示文本字段当前是否处于编辑模式。 (只读)
例如,您有 3 个文本字段,那么解雇键盘将如下所示:
You should only send
dismissKeyboard
to thattextField
that you are currently editing.In your code you have got memory leak. Better use this one:
To check if
UITextField
is currently inedit mode
you can check its property:A Boolean value indicating whether the text field is currently in edit mode. (read-only)
For example, you have 3 text fields then dismissKeyboard will look something like this:
我在许多应用程序中使用相同的功能。我没有使用 GestureRecognizer,而是将视图设置为
UIControl
,而不是UIView
。您仍然可以执行使用UIView
执行的操作,但您也可以指定在与视图交互时执行的IBActions
。操作方法如下:
在 Interface Builder 中,选择您的视图。然后,将其类分配给
UIControl
。 (当前可能设置为 UIView。在该视图的 ViewController 中,编写一个 IBAction 方法来检测背景Taps。我的看起来像这样:
最后,在 Interface Builder 中,将您创建的 IBAction 连接到 <代码>UIControl。
I use the same functionality in many of my apps. Rather than using the GestureRecognizer, I set my view up as a
UIControl
, rather than aUIView
. You can still do the things you'd do with aUIView
, but you can also assignIBActions
to be performed when interacting with the view.Here's how to do it:
In Interface Builder, select your view. Then, assign its class to
UIControl
. (It's probably set up asUIView
currently.In your ViewController for that view, write an IBAction method to detect backgroundTaps. Mine looks like this:
Finally, in Interface Builder, connect the IBAction you created to the
UIControl
.这里我给出了常见的文本字段对象。并在“textFieldShouldBeginEditing”方法中指定对其的引用。这对于所有文本字段都是通用的。
在这种情况下,您需要关闭一个将隐藏键盘的文本字段。
在 .h 文件中声明文本字段对象。
在 .m 文件中
Here i give common text field object. and asign reference to it in "textFieldShouldBeginEditing" method. that will common for all text field..
In this case, you need to dismiss one text field that will hide keyboard..
declare textfield object in .h file.
IN .m file