objective-c的TextFields输入完成后关闭键盘和触摸背景关闭键盘

首先输入完成后按键盘上的done关闭键盘

首先在Interface Builder中选择TextFields,然后在Text Field Attributes中找到Text Input Traits,选择Return Key为done。OK

定义方法

- (IBAction) textFieldDoneEditing:(id)sender; //按下Done键关闭键盘

实现方法

//按完Done键以后关闭键盘

- (IBAction) textFieldDoneEditing:(id)sender

{

[sender resignFirstResponder];

}

然后找到事件Did End On Exit,与textFieldDoneEditing关联,OK。

如果是数字键盘,没有done键怎么办呢,我们通过触摸背景关闭键盘

定义方法

- (IBAction) backgroundTap:(id)sender; //通过触摸背景关闭键盘

实现方法

//通过触摸背景关闭键盘

- (IBAction) backgroundTap:(id)sender

{

[nameFiled resignFirstResponder];

[numberField resignFirstResponder];

}

然后选择背景的Touch Down事件,关联 backgroundTap,OK