IOS - 如果用户点击randome元素而不是背景,如何隐藏键盘? [英] IOS - how to hide keyboard if user clicks on randome elements and not background?

查看:78
本文介绍了IOS - 如果用户点击randome元素而不是背景,如何隐藏键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有这样的东西:

In my app I have something like this:

- (IBAction)backgroundTouch:(id)sender 
{
    [businessDescription resignFirstResponder];
    [self.view endEditing:YES]; 
}

我不确定我使用哪两条线路更好所以我使用两者:)当文本区域突出显示,并且用户按下背景时它起作用。

I am not sure which of the two lines I use is better so I use both :) It works when the text area is highlighted, and the user presses the background.

但用户并不总是按背景,有时按其他页面元素就像他们试图填写的下一个元素一样。

But users do not always press the background, and sometimes press on other page elements like the next element they are trying to fill out.

在我的屏幕截图中,我在文本区域下面有下一个元素,当我点击那里时,键盘没有不被隐藏。当用户点击各种页面元素时,以及当textarea没有突出显示时,有人可以帮我隐藏键盘吗?

In my screen shot, I have the next element below the text area, and when I click there, the keyboard doesn't get hidden. Could anyone help me hide the keyboard when the user clicks on various page elements, and when the textarea happens not to be highlighted?

这是我的.h文件:

@interface PlanBusinessController : UIViewController

@property (weak, nonatomic) IBOutlet UITextView *businessDescription;

- (IBAction)submitBusiness:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *buttonProperty;

@property (weak, nonatomic) IBOutlet UITextField *personName;

@property (weak, nonatomic) IBOutlet UITextField *personEmail;

@property (weak, nonatomic) 
    IBOutlet UISwitch *privacy;

@property (weak, nonatomic) IBOutlet UISwitch *wantHelp;

- (IBAction)helpToggle:(id)sender;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *emailLabel;

@property (weak, nonatomic) IBOutlet 
UIButton *test;

@end

这是我的.m声明:

#import "PlanBusinessController.h"

@interface PlanBusinessController ()

@end

@implementation PlanBusinessController
@synthesize nameLabel;
@synthesize emailLabel;
@synthesize businessDescription;
@synthesize buttonProperty;
@synthesize personName;
@synthesize personEmail;
@synthesize privacy;
@synthesize wantHelp;
@synthesize test;


-(void)touchesBegan:(NSSet*)touches
{
    UITouch *touch=[touches anyObject];
    UIView *view=touch.view;
    if (![view isEqual:businessDescription])
    {
        //[businessDescription resignFirstReponder];
    }
}

- (IBAction)backgroundTouch:(id)sender 
{
    [businessDescription resignFirstResponder];
    [self.view endEditing:YES]; 
}

谢谢!

推荐答案

我在我的程序ViewController中使用此方法,它工作正常。我试一试。

I use this method in my program ViewController and it works fine. I'd give it a try.

//Used with the text fields to dismiss keyboard
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [textField resignFirstResponder];
}

如果你有其他元素,那么在这些元素的方法中也添加 [textField resignFirstResponder]

If you have other elements, then in the methods for those elements also add [textField resignFirstResponder].

例如,如果他们可以点击按钮,请写下这样的内容:

For example, if they can click on a button, write something like this:

-(IBAction)button1:(id)sender
{   
    [textField resignFirstResponder];

     //Do stuff
}

注意:你需要每个要关闭的文本字段都有一个。例如:

Note: You need one for each textfield you want to close. For example:

-(IBAction)button1:(id)sender
{   
    [textField resignFirstResponder];
    [textField2 resignFirstResponder];
    []... etc

     //Do stuff
}

这篇关于IOS - 如果用户点击randome元素而不是背景,如何隐藏键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆