触摸UIControl时如何关闭键盘?

时间:2014-01-25 13:26:54

标签: ios uisegmentedcontrol uidatepicker uicontrol

这已被问过几次,但我似乎没有得到它。

我有一个视图控制器,有两个文本字段,一个分段控件,一个日期选择器和一些标签。

当用户点击背景或分段控件或日期选择器时,我想关闭键盘。 这是我的.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,我该如何解除键盘?

5 个答案:

答案 0 :(得分:3)

你触摸代码是正确的,但它在我的代码中工作正常。当你在那个时候点击文本字段之外触摸事件调用但它的内部条件不能正常工作。我删除条件并检查其工作正常。

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

enter image description here

答案 1 :(得分:1)

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[geburtsdatumPicker addGestureRecognizer:tap];
[genderSegmentedControl addGestureRecognizer:tap];
[self.view addGestureRecognizer:tap];

然后在选择器内重新设置键盘

-(void) dismissKeyboard
{
    [nachnameTextField resignFirstResponder];
    [vornameTextField resignFirstResponder];
}

答案 2 :(得分:0)

您是否尝试过以下代码行

[textfieldInFocus resignFirstResponder];

答案 3 :(得分:0)

您可以执行以下操作:

UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
tapGesture.delegate=self;
[self.view addGestureRecognizer:tapGesture];

- (void)tapAction
{
    [nachnameTextField resignFirstResponder];
    [vornameTextField resignFirstResponder];
}

对于UISegmentedControlUIDatePicker,您可能拥有自己的选择器方法,例如: 对于UISegmentedControl

- (IBAction)mainSegmentControl:(UISegmentedControl *)segment{
}

UIDatePicker

- (void)changeDate:(UIDatePicker *)sender {
}

只需在上述两种方法中调用您的方法[self tapAction];,您就可以开始...... !!

答案 4 :(得分:0)

这是适合我的解决方案:MRViewController.h文件:

#import <UIKit/UIKit.h>
@interface MRViewController : UIViewController <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *textField;
@property (strong, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
@end

MRViewController.m文件:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self hideKeyboardWhenBackgroundIsTapped];
    self.textField.delegate = self;
}
-(void)hideKeyboardWhenBackgroundIsTapped{
    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [tgr setCancelsTouchesInView:NO];
    [self.view addGestureRecognizer:tgr];
}
-(void)hideKeyboard{
    [self.view endEditing:YES];
}

在我的故事板场景中,我得到了一个UITextField和一个UISegmentedControl。

相关问题