你在这里。 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];
}
当您显示PickerView时不能隐藏键盘吗? – ilight
https://docs.google.com/file/d/0B9zRYaEUNwXMUF8zRUNhTW1xRWc/edit?pli = 1 textfield和picker视图被放置在同一个屏幕 –
你可以用另一种方式思考它..当文本框结束编辑时隐藏键盘..我认为这对你的情况更好。 –