@zwenqiang
2015-10-10T18:02:17.000000Z
字数 1164
阅读 2376
IOS
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.roomField resignFirstResponder];
[self.buildField resignFirstResponder];
}
self.roomField.delegate = self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
// called when 'return' key pressed. return NO to ignore.
NSLog(@"按下了return键");
[textField resignFirstResponder];
return YES;
}
当用户按下Return按钮时,将触发“did end on exit”事件,此时我们需要让文本字段取消控件,以关闭键盘。
[self.roomField addTarget:self action:@selector(closeTextFieldKeyBorad:) forControlEvents:UIControlEventEditingDidEndOnExit];
# 或者在Interface Builder中按command+2 选择Did End on Exit旁边的圆圈拖到File’s Owner图标,并将它连接到closeTextFieldKeyBorad操作。
- (void)closeTextFieldKeyBorad: (id)sender{
[sender resignFirstResponder];
NSLog(@"关闭键盘%@",sender);
}
UIControl *control = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
[control addTarget:self action:@selector(closeTextFieldKeyBorad:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:control];
- (void)closeTextFieldKeyBorad: (id)sender{
NSLog(@"开始关闭键盘");
[self.roomField resignFirstResponder];
NSLog(@"关闭键盘%@",sender);
}