我使用UIPicker填充UITextField而不是键盘。这很好但我现在无法关闭UIPicker。我希望能够点击屏幕上的任意位置并关闭UIPicker。我已经尝试了每种触摸方法,无人会触发。
setUserInteractionEnabled:YES。 不确定它是否有所作为,但我使用的是StoryBoard
我应该在AppDelegate中设置一些内容来监听触摸吗?
这是我的.h
#import <UIKit/UIKit.h>
@interface RNMemberTableViewController : UITableViewController<UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *behaviorPicker;
@property (nonatomic, weak) IBOutlet UIDatePicker *dateOfRecordPicker;
@property (nonatomic, strong) NSArray *behaviorLevels;
@property (weak, nonatomic) IBOutlet UITextField *behaviorTextField;
@end
这里有一些实现......
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setUserInteractionEnabled:YES];
[self buildBehaviorPicker];
NSLog(@"Member View");
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"test");
[self.view touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
int tCount = touch.tapCount;
NSLog(@"Touched %d", tCount);
}
- (void) buildBehaviorPicker
{
behaviorLevels = [[NSArray alloc] initWithObjects:@"Unsatisfactory", @"Needs Improvement", @"Satisfactory", @"Outstanding", nil];
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.dataSource = self;
pickerView.delegate = self;
[pickerView selectRow:2 inComponent:0 animated:NO];
self.behaviorTextField.inputView = pickerView;
}
提前致谢 -Bob
答案 0 :(得分:6)
您可以随时尝试以下操作:
-(void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapReceived:)];
[tapGestureRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapGestureRecognizer];
}
-(void)tapReceived:(UITapGestureRecognizer *)tapGestureRecognizer
{
// do something, like dismiss your view controller, picker, etc., etc.
}
希望这会有所帮助......
答案 1 :(得分:0)
您可以添加一个与选择器下控制器视图大小相同的透明按钮。
显示选择器时设置button.hidden = NO
,并在隐藏选择器时设置button.hidden = YES
。
有一些方法可以隐藏选择器。最简单的方法是设置picker.hidden = YES
。
答案 2 :(得分:-5)
如果您正在使用UIScrollView
a
将不会被调用,因为touchesBegan是UIView的方法,而不是UIScrollView的方法。