如果你用interface builder,那么下面的这片文章很值得参考:
http://www.howzhi.com/group/iosDevelop/discuss/1880
如果是用村纯代码:
先实现UITextFiledDelegate:
@interface LoginPageViewController : UIViewController <UITextFieldDelegate>
加入如下方法:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[usernameTextFiled resignFirstResponder ];
[passwdTextFiled resignFirstResponder ];
}
usernameTextFiled.delegate = self;
passwdTextFiled.delegate = self;
也可以用GestureRecongnizer实现:
- (void)viewDidLoad
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backupgroupTap:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[self.view addGestureRecognizer: tapGestureRecognizer]; //只需要点击非文字输入区域就会响应
[tapGestureRecognizer setCancelsTouchesInView:NO];
}
-(void)backupgroupTap:(id)sender{
[textField1 resignFirstResponder]; //关闭所有UITextField控件的键盘。。。
[textField2 resignFirstResponder];
}