我使用UIPicker来填充UITextField,而不是键盘。这很好用,但是我现在不能关闭UIPicker。我希望能够在屏幕上的任何位置点击并关闭UIPicker。我已经尝试了每一种都不会触发的触摸方法。
setUserInteractionEnabled:可以。我不确定这是否有区别,但我使用的是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
发布于 2014-07-08 22:41:16
您可以随时尝试以下操作:
-(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.
}希望这能帮到你。
发布于 2014-07-08 10:58:32
您可以在选取器下添加一个与控制器视图大小相同的透明按钮。
显示选取器时设置button.hidden = NO,隐藏选取器时设置button.hidden = YES。
有几种方法可以隐藏选择器。最简单的方法是设置picker.hidden = YES。
发布于 2015-07-23 18:43:23
如果您使用的是UIScrollView,那么
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event将不会被调用,因为touchesBegan是UIView的方法而不是UIScrollView的方法。
https://stackoverflow.com/questions/24621402
复制相似问题