gpt4 book ai didi

ios - 触摸 UIControl 时如何关闭键盘?

转载 作者:行者123 更新时间:2023-11-28 19:04:54 26 4
gpt4 key购买 nike

这已被问过几次,但我似乎不明白。

我有一个 View Controller ,有两个文本字段、一个分段控件、一个日期选择器和一些标签。

我想在用户单击背景或分段控件或日期选择器时关闭键盘。这是我的 .h 文件:

@interface MRPatientenViewController : UIViewController
@property (nonatomic, strong) MRPatientenTableViewController *delegate;
- (IBAction)textFieldReturn:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *nachnameTextField;
@property (strong, nonatomic) IBOutlet UITextField *vornameTextField;
@property (strong, nonatomic) IBOutlet UIDatePicker *geburtsdatumPicker;
@property (strong, nonatomic) IBOutlet UISegmentedControl *genderSegmentedControl;
@end

这是我的.m 文件:

-(IBAction)textFieldReturn:(id)sender{
[sender resignFirstResponder];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([_nachnameTextField isFirstResponder] && [touch view] != _nachnameTextField) {
[_nachnameTextField resignFirstResponder];
}
else if ([_vornameTextField isFirstResponder] && [touch view] != _vornameTextField) {
[_vornameTextField resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}

现在,如果触摸到背景或标签,这将关闭键盘。

但是,如果触摸了 UISegmentedControldatepicker,我该如何关闭键盘呢?

最佳答案

您的 Touch 代码是正确的,但它在我的代码中也能正常工作。当您当时在文本字段外部单击时,Touch 事件调用但其内部条件不正常。我删除该条件并检查其工作正常。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[nachnameTextField resignFirstResponder];
[super touchesBegan:touches withEvent:event];
}

enter image description here

关于ios - 触摸 UIControl 时如何关闭键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21351143/

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