gpt4 book ai didi

ios - UITapGestureRecognizer 并在点击背景时使键盘消失

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:25 26 4
gpt4 key购买 nike

我创建了一个表格,输入年龄等数据时会出现小键盘(仅限数字)。

我希望当用户点击背景时键盘消失,并且我想在 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 个文本字段。我是否需要在 dismissKeyboard 方法中编写每个文本字段?

最佳答案

简单的方法是使用 UIView 中提供的方法

- (BOOL) endEditing:(BOOL)force;

This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.

所以就这样做:

-(void)dismissKeyboard {
[self.view endEditing:YES];
}

并且它将支持您在页面上添加的任何更多文本字段(当然在该 UIView 下)

关于ios - UITapGestureRecognizer 并在点击背景时使键盘消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7324748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com