touchesBegan not being called
16,605
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 ...
Author by
Bob
Updated on June 04, 2022Comments
-
Bob almost 2 years
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
-
Bob almost 10 yearsThis worked great but any idea why touchesBegan won't fire? Is this best practice? I'm very new to Obj-C
-
Bob almost 10 yearsI'd still like to know why the touches method aren't firing
-
BonanzaDriver almost 10 yearsIf you set this up on a regular (non-table view controller) you'll see different results using touchesBegan() - the table is catching. But, the right way to do this is still using a gesture recognizer.
-
Alan Scarpa over 8 yearsTo make this code work, you need your view controller to conform to this delegate too: <UIGestureRecognizerDelegate>
-
davew over 8 yearsActually touchesBegan is a method of UIResponder. UIView inherits from UIResponder and UIScrollView inherits from UIView, so the reasoning here can't be accurate.
-
KoreanXcodeWorker over 5 years@Alan_s Not needed at all.
-
Rakshitha Muranga Rodrigo over 2 yearsYes, This seems to be the correct way to implement this scenario, rather than using
UIResponder