关闭键盘 – iOS 7中的多个UITextFields

前端之家收集整理的这篇文章主要介绍了关闭键盘 – iOS 7中的多个UITextFields前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面你会发现我的.h& .m文件为我的主视图控制器.

我有3个问题.

因为我有多个uitextfields,我必须设置每个自己的resignFirstResponder语句?和2.)我该怎么办,用什么方法?我的语法是否适合辞退第一位回复者?

此外,如果用户点击该字段而不是按返回键,我可以解除键盘,这将是非常好的!

我知道这已经被问到和回答了,但老实说,我还是有点困惑,在哪里.

我正在使用XCode 5和iOS 7的故事板.

=============================

.h文件

@interface ViewController : UIViewController <UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property (weak,nonatomic) IBOutlet UITextField *danceDate;
@property (weak,nonatomic) IBOutlet UITextField *dancePlace;
@property (weak,nonatomic) IBOutlet UITextField *danceTerminal;
@property (weak,nonatomic) IBOutlet UITextField *danceGate;

.m文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self retrieveFromParse];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    // SET DELEGATE HERE
    //
    // if I uncomment 1 of these lines,i'll get an error.
    //
    // _dancePlace.delegate = self; 
    // dancePlace.delegate = self; 
    // dancePlace = self; 

}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{

}

-(BOOL) textFieldShouldReturn: (UITextField *) textField
{
    [textField resignFirstResponder];

    return YES;
}


-(BOOL) textFieldShouldReturn: (UITextField *) textField
{
    return YES;
}

解决方法

调换textField:
您的所有textField.delegate都应设置为ViewController的对象.
后执行下面的代理方法.
-(BOOL) textFieldShouldReturn: (UITextField *) textField {
[textField resignFirstResponder];
    return YES;
}

点击视图关闭键盘
将点击手势添加到ViewController.view中,如下所示:

//declare a property to store your current responder
@property (nonatomic,assign) id currentResponder;


//in viewDidLoad:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignOnTap:)];
    [singleTap setNumberOfTapsrequired:1];
    [singleTap setNumberOfTouchesrequired:1];
    [self.view addGestureRecognizer:singleTap];
    [singleTap release];

//Implement the below delegate method:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.currentResponder = textField;
}

//Implement resignOnTap:

- (void)resignOnTap:(id)iSender {
    [self.currentResponder resignFirstResponder];
}
// was missing ; after the call --> [self.currentResponder resignFirstResponder]
    // also in textFieldDidEndEditing set self.currentResponder = nil;

猜你在找的iOS相关文章