2

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

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

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

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

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

4

3 回答 3

2

有解决办法

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

2)创建一个内部动作的修饰

 - (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) 完成

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

于 2012-06-11T10:26:13.500 回答
1

我在我的代码中使用它。我爬上响应者链并重新分配响应者。我把它放在显示pickerView的方法中。到目前为止没有意外的问题。如果键盘正在显示,似乎可以工作,如果没有键盘显示,似乎不会崩溃。

    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
于 2012-10-26T19:56:38.757 回答
0

干得好。UIPickerViews 是一个相当复杂的嵌套 UIViews 系统,这就是为什么您没有从该touchesBegan:withEvent:方法获得任何响应的原因。你可以做的是创建你的 UIPickerView 子类,如下所示:

//
//  MyPickerView.h
//

#import <UIKit/UIKit.h>

// Protocol Definition that extends UIPickerViewDelegate and adds a method to indicate a touch
@protocol MyPickerViewDelegate <UIPickerViewDelegate>

// This is the method we'll call when we've received a touch. Our view controller should implement it and hide the keyboard
- (void)pickerViewDidReceiveTouch:(UIPickerView *)pickerView;

@end

@interface MyPickerView : UIPickerView

// We're redefining delegate to require conformity to the MyPickerViewDelegate protocol we just made
@property (nonatomic, weak) id <MyPickerViewDelegate>delegate;

@end


//
//  MyPickerView.m
//

#import "MyPickerView.h"

@implementation MyPickerView
@synthesize delegate = _myPickerViewDelegate; // We changed the data type of delegate as it was declared in the superclass so it's important to link it to a differently named backing variable

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    // We make sure to call the super method so all standard functionality is preserved
    UIView *hitView = [super hitTest:point withEvent:event];

    if (hitView) {
        // This will be true if the hit was inside of the picker
        [_myPickerViewDelegate pickerViewDidReceiveTouch:self];
    }

    // Return our results, again as part of preserving our superclass functionality
    return hitView;
}

@end

然后在您的 ViewController 中将其更改为符合<MyPickerViewDelegate>而不是<UIPickerViewDelegate>. 这没关系,因为MyPickerViewDelegate继承自标准方法UIPickerViewDelegate并将通过标准UIPickerViewDelegate方法。

最后pickerViewDidReceiveTouch:在您的视图控制器中实现:

- (void)pickerViewDidReceiveTouch:(UIPickerView *)pickerView {
    [enterInput resignFirstResponder];
}
于 2012-05-30T07:54:37.723 回答