gpt4 book ai didi

IOS - 触摸 UIPickerView 时关闭键盘

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:15:27 26 4
gpt4 key购买 nike

我正在构建一个转换器应用程序。在主屏幕中,我有一个用于输入数字的文本字段,文本字段下方的选择器 View 将允许用户选择转换参数(例如 kg 到 g)。

我可以使用以下方法在用户单击背景时隐藏键盘

 (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.enterInput resignFirstResponder];

但是当我触摸选择器 View 时,键盘没有隐藏。

我的问题是如何在用户触摸选择器 View 时关闭键盘。

最佳答案

找到解决方案

1) 首先创建一个隐藏的 roundRect 按钮,并将类型更改为自定义(适合选择器的大小)。

2) 创建内部 Action 的修饰

 - (IBAction)hiddenButtonToHideKeyboard:(id)sender {
[self.enterInput resignFirstResponder];
}

3) 创建键盘出现通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardAppear:) name:UIKeyboardWillShowNotification object:nil];

4) 创建键盘消失通知

 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardHide:) name:UIKeyboardWillHideNotification object:nil];

5) 当键盘出现时使按钮可见

 -(void)onKeyboardAppear:(NSNotification *)notification
{
hiddenButtonToHideKeyboard.hidden=NO;
}

6) 键盘消失时隐藏按钮

-(void)onKeyboardHide:(NSNotification *)notification
{
hiddenButtonToHideKeyboard.hidden=YES;
}

5)完成

我不认为这是一个完美的解决方案,但它对我有用:)

关于IOS - 触摸 UIPickerView 时关闭键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10796657/

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