我试图在屏幕上的任何其他地方触摸后隐藏键盘.我正在使用的代码基于这里的答案.
IBOutlet UITextView *myTextView;
Run Code Online (Sandbox Code Playgroud)
方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([myTextView isFirstResponder] && [touch view] != myTextView) {
[myTextView resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)
我不明白的是我应该如何将我的UITextField链接到该touchesBegan方法.我需要使用哪个发送的事件?此外,该方法不应该是IBAction,因为现在我无法将我的UITextField连接到它.
Dir*_*ctX 26
目标C:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:YES];
}
Run Code Online (Sandbox Code Playgroud)
迅速:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
Run Code Online (Sandbox Code Playgroud)
这是我找到的最好的方式,而且非常简单.
小智 6
我应该如何将我的UITextField链接到touchesBegan方法.我需要使用哪个发送的事件?此外,该方法不应该是IBAction,因为现在我无法将我的UITextField连接到它.
因为你没有.您必须在文本字段为子视图的视图上覆盖此方法.
| 归档时间: |
|
| 查看次数: |
13173 次 |
| 最近记录: |