gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:39:11 25 4
gpt4 key购买 nike

我在一个屏幕上有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com