iPhone dismiss keyboard

在ios7.1环境下,好像UITextField的键盘不会自动收起来,最后在stackover上找到办法

首先要有View或者ViewController实现UITextFieldDelegate

@interface LoginView : UIView<UITextFieldDelegate>


1、按下键盘的return键,收起键盘

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

2、触摸屏幕其他位置,收起键盘

{
    id currentResponder;
}

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

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignOnTap)];
[singleTap setNumberOfTapsRequired:1];// 触摸一次
[singleTap setNumberOfTouchesRequired:1];// 单指触摸
[self addGestureRecognizer:singleTap];
-(void) resignOnTap
{
    [currentResponder resignFirstResponder];
}

关键就是resignFirstResponder方法,但是触发时机不同

第一个场景,触摸键盘的return键时,会调用delegate的方法,然后调用resignFirstResponder

第二个场景,在整个view上注册了手势识别,当触摸到屏幕的其他位置时,调用resignFirstResponder

你可能感兴趣的:(iPhone dismiss keyboard)