触摸textField以外的地方,键盘回收(两种方法)

来源:互联网 发布:淘宝怎么批量上架 编辑:程序博客网 时间:2024/06/04 23:16
第一种:
创建一个不可见的button,将其放在其他所有元素后面,然后在该button的触发事件中写resignFirstResponder
具体分四步:
1、拖一个round rect button到视图窗口,调整大小使其占据整个屏幕。
2、从xcode菜单中选择send to back,使该button置于后面。
3、将该button的类型改为custom,使其失去round rect button的特性。
4、写一个backgroundClick方法,使其作为该button的响应函数。

第二种(更高效的一种方法):
实现touchesBegan方法来隐藏键盘:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

[super touchesBegan:touches withEvent:event];

[textField resignFirstResponder];

self.label.text = textField.text; //do some other things.

}

0 0