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

发布于 2024-12-03 06:06:54 字数 647 浏览 2 评论 0原文

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

我希望当用户点击背景时键盘消失,并且我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情未る 2024-12-10 06:06:54

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

- (BOOL) endEditing:(BOOL)force;

此方法查看当前视图及其子视图层次结构,以查找当前作为第一响应者的文本字段。如果找到,它会要求该文本字段辞去第一响应者的角色。如果强制参数设置为 YES,则根本不会询问文本字段;被迫辞职。

所以只需这样做:

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

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

Easy way to do this is to use the method provided in 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.

So just do this:

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

and it will support any more text fields you add on your page (under that UIView of course)

故事与诗 2024-12-10 06:06:54

您应该只将 dismissKeyboard 发送到您当前正在编辑的 textField

在您的代码中,您遇到了内存泄漏。更好地使用这个:

-(void)viewDidLoad
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap]; 
    [tap release];
}

要检查 UITextField 当前是否处于编辑模式,您可以检查其属性:

一个布尔值,指示文本字段当前是否处于编辑模式。 (只读)

@property(nonatomic, readonly, getter=isEditing) BOOL editing

例如,您有 3 个文本字段,那么解雇键盘将如下所示:

-(void)dismissKeyboard
{
    UITextField *activeTextField = nil;
    if ([textField1 isEditing]) activeTextField = textField1;
    else if ([textField2 isEditing]) activeTextField = textField2;
    else if ([textField3 isEditing]) activeTextField = textField3;
    if (activeTextField) [activeTextField resignFirstResponder];
}

You should only send dismissKeyboard to that textField that you are currently editing.

In your code you have got memory leak. Better use this one:

-(void)viewDidLoad
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap]; 
    [tap release];
}

To check if UITextField is currently in edit mode you can check its property:

A Boolean value indicating whether the text field is currently in edit mode. (read-only)

@property(nonatomic, readonly, getter=isEditing) BOOL editing

For example, you have 3 text fields then dismissKeyboard will look something like this:

-(void)dismissKeyboard
{
    UITextField *activeTextField = nil;
    if ([textField1 isEditing]) activeTextField = textField1;
    else if ([textField2 isEditing]) activeTextField = textField2;
    else if ([textField3 isEditing]) activeTextField = textField3;
    if (activeTextField) [activeTextField resignFirstResponder];
}
給妳壹絲溫柔 2024-12-10 06:06:54

我在许多应用程序中使用相同的功能。我没有使用 GestureRecognizer,而是将视图设置为 UIControl,而不是 UIView。您仍然可以执行使用 UIView 执行的操作,但您也可以指定在与视图交互时执行的 IBActions

操作方法如下:
在 Interface Builder 中,选择您的视图。然后,将其类分配给UIControl。 (当前可能设置为 UIView。

在该视图的 ViewController 中,编写一个 IBAction 方法来检测背景Taps。我的看起来像这样:

- (IBAction)backgroundTap:(id)sender
{
    if ([textField1 isEditing]) {
        [textField1 resignFirstResponder];
    } else if ([textField2 isEditing]) {
        [textField2 resignFirstResponder];
    }
}

最后,在 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 a UIView. You can still do the things you'd do with a UIView, but you can also assign IBActions 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 as UIView currently.

In your ViewController for that view, write an IBAction method to detect backgroundTaps. Mine looks like this:

- (IBAction)backgroundTap:(id)sender
{
    if ([textField1 isEditing]) {
        [textField1 resignFirstResponder];
    } else if ([textField2 isEditing]) {
        [textField2 resignFirstResponder];
    }
}

Finally, in Interface Builder, connect the IBAction you created to the UIControl.

扮仙女 2024-12-10 06:06:54

这里我给出了常见的文本字段对象。并在“textFieldShouldBeginEditing”方法中指定对其的引用。这对于所有文本字段都是通用的。

在这种情况下,您需要关闭一个将隐藏键盘的文本字段。

在 .h 文件中声明文本字段对象。

@interface RootViewController : UIViewController
{
    IBOutlet UITextField* txt_common;
}

在 .m 文件中

- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap]; 
[tap release];


}

-(void)dismissKeyboard
{
NSLog(@"hi");
[txt_common resignFirstResponder];
}

#pragma mark TextField methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"hi++++++++++++++++++");

txt_common=textField;
return YES;
}

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.

@interface RootViewController : UIViewController
{
    IBOutlet UITextField* txt_common;
}

IN .m file

- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap]; 
[tap release];


}

-(void)dismissKeyboard
{
NSLog(@"hi");
[txt_common resignFirstResponder];
}

#pragma mark TextField methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"hi++++++++++++++++++");

txt_common=textField;
return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文