一.使用OC
1.首先, 要添加代理 UITextFieldDelegate
2.设置代理 textField.delegate = self;
3.实现协议中的方法
//点击return收回键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//回收键盘,取消第一响应者
[textField resignFirstResponder];
return
YES;
}
//点击空白处收回键盘
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[textField resignFirstResponder];
}
二.使用Swift
步骤1, 2和OC中无差别, 具体实现方法稍有差别
1.点击return回收键盘
func
textFieldShouldReturn(textField: UITextField!) -> Bool{
textField.resignFirstResponder()
return true
}
2.点击空白回收键盘
var firstTextField : UITextField?
var secondTextField : UITextField?
var thirdTextField : UITextField?
override func touchesEnded(touches: Set, withEvent event: UIEvent)
{
thirdTextField?.resignFirstResponder()
firstTextField?.resignFirstResponder()
secondTextField?.resignFirstResponder()
}