touchesBegan not being called
ios, objective-c, uiview
Solution
You can always try the following:
-(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.
}
Hope this helps ...
Problem
I'm using a UIPicker to populate a UITextField instead of a keyboard. This works great but I can't close the UIPicker now. I want to be able to tap anywhere on the screen and close the UIPicker. I've tried each of the touches method an none will fire. setUserInteractionEnabled:YES. Not sure if it makes a difference but I'm using a StoryBoard Should I have set something up in my AppDelegate to listen for touches? Here is my .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 ``` here is some of the implementation... ``` - (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; } ``` Thanks in advance -Bob