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 ...

Share:
16,605
Bob
Author by

Bob

Updated on June 04, 2022

Comments

  • Bob
    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
    Bob almost 10 years
    This worked great but any idea why touchesBegan won't fire? Is this best practice? I'm very new to Obj-C
  • Bob
    Bob almost 10 years
    I'd still like to know why the touches method aren't firing
  • BonanzaDriver
    BonanzaDriver almost 10 years
    If 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
    Alan Scarpa over 8 years
    To make this code work, you need your view controller to conform to this delegate too: <UIGestureRecognizerDelegate>
  • davew
    davew over 8 years
    Actually touchesBegan is a method of UIResponder. UIView inherits from UIResponder and UIScrollView inherits from UIView, so the reasoning here can't be accurate.
  • KoreanXcodeWorker
    KoreanXcodeWorker over 5 years
    @Alan_s Not needed at all.
  • Rakshitha Muranga Rodrigo
    Rakshitha Muranga Rodrigo over 2 years
    Yes, This seems to be the correct way to implement this scenario, rather than using UIResponder