2012-05-29 47 views
2

我正在构建一个转换器应用程序。在主屏幕中,我有一个文本字段用于输入数字,在文本字段下方,选取器视图允许用户选择转换参数(例如kg到g)。IOS - 触摸UIPickerView时关闭键盘

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

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

但是当我触摸选取器查看该键盘没有隐瞒。

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

+0

当您显示PickerView时不能隐藏键盘吗? – ilight

+0

https://docs.google.com/file/d/0B9zRYaEUNwXMUF8zRUNhTW1xRWc/edit?pli = 1 textfield和picker视图被放置在同一个屏幕 –

+0

你可以用另一种方式思考它..当文本框结束编辑时隐藏键盘..我认为这对你的情况更好。 –

回答

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)隐藏)完成

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

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 

然后在您的视图控制器更改为符合<MyPickerViewDelegate>而不是<UIPickerViewDelegate>。这是可以的,因为MyPickerViewDelegate继承自UIPickerViewDelegate并且将通过标准UIPickerViewDelegate方法。

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

- (void)pickerViewDidReceiveTouch:(UIPickerView *)pickerView { 
    [enterInput resignFirstResponder]; 
} 
+0

嗨,谢谢你的回答, –

+0

@Clement很高兴帮助。希望它为你工作。如果确实如此,请确保将答案标记为正确。谢谢。 –

1

当键盘消失

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

5按钮我在我的代码中使用了这个。我爬上响应者链并重新分配响应者。我把它放在显示pickerView的方法中。至今没有意外的问题。似乎工作,如果键盘显示,似乎不会崩溃,如果没有键盘显示。

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];