ios - 在多个 View 之一中关闭键盘

标签 ios objective-c keyboard

我在一个屏幕上有 3 个 View ,其中包含不同的组件,例如表格和按钮,当我尝试使用下面的代码关闭键盘时,当我单击包含 textView 的 View 时它不会关闭(这就是我需要! ) 。但是,当我单击其中一个其他 View 时,它会消失。

下图显示了我拥有的 View , ScrollView 中的最后一个 View 是包含 TextView 的 View 。

enter image description here

尽管录制了 View ,我怎样才能让键盘关闭。

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

UITouch * touch = [touches anyObject];

if(touch.phase == UITouchPhaseBegan) {
    [aTextField resignFirstResponder];
  }
}

此代码也无法消除:

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

[[self view] endEditing:TRUE];

}

最佳答案

您需要在要单击的 View 中添加点击手势: 因为您正在使用两个名为 Your_view1 和 Your_view2 的 UIView,文本字段为 txt1 和 txt2。代码如下:

Your_view1:

UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
singleTapGesture.numberOfTapsRequired = 1;
[Your_view1 addGestureRecognizer:singleTapGesture];

你的观点2:

UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
singleTapGesture.numberOfTapsRequired = 1;
[Your_view2 addGestureRecognizer:singleTapGesture];

代表是:

-(void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTapGesture{
    [txt1 resignFirstResponder];
    [txt2 resignFirstResponder];
}

关于ios - 在多个 View 之一中关闭键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47543016/

相关文章:

ios - 如何调试 iOS 框架 dyld "lazy symbol binding failed"崩溃

objective-c - iOS7 上的 UIActivityViewController 中未显示 Facebook、Twitter 图标

objective-c - 如何列出连接到 Mac 的 USB 端口的设备?

javascript - 检测 MAC javascript 中的命令键

ios - 如何使用默认值在 alertView 中创建 tableView?

ios - 如何从键盘打印格式化文本

ios - 问题 iOS 框架大小

ios - Swift - 在用户输入时获取键盘输入

ios - 刷新键盘而不辞职并成为第一响应者

ios - Xcode build设置中的替代安装权限是什么?