2

In my app, i have pickerviews in my add contact page. I have given the following 'touchesBegan' method. But it dismisses the picker even when i click on a value to set. For example, if i click 'homephone' from preferred phone picker, the picker gets dismissed instead of setting the value. i need the picker to be dismissed only if i click outside the picker. Can anyone help me in fixing this.

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 [self.AddView endEditing:YES];
}
Mano
  • 670
  • 1
  • 9
  • 28

3 Answers3

6

You can use

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch *touch = [touches anyObject];
  if ([touch view] != pickerview)
     [self.AddView endEditing:YES];
}

this will be used to endedit when the touch is outside the picker view

2

In your code:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

 [self dismissViewControllerAnimated:YES completion:nil];

}

Or You can put a bigger button under the PickerView, make it color clear custom and dimension full screen, then use the dismiss inside a (IBAction) and when you tap outside the picker your view is dismissed

- (IBAction)buttonClear:(id)sender {

[self dismissViewControllerAnimated:YES completion:nil];

}
BlackSheep
  • 1,087
  • 12
  • 29
  • put a bigger button under the PickerView, make it color clear custom and dimension full screen, then use the dismiss inside a (IBAction) and when you tap outside the picker your view is dismissed ;) i add a code on my post – BlackSheep Jul 04 '13 at 09:37
2

try this code:

UITapGestureRecognizer *single_tap_recognizer;

    single_tap_recognizer = [[[UITapGestureRecognizer alloc]
                              initWithTarget : self.view
                              action         : @selector(upper_button_view_tapped)]
                             autorelease];

    [single_tap_recognizer setNumberOfTouchesRequired : 1];
    [self.view addGestureRecognizer : single_tap_recognizer];

-(void)upper_button_view_tapped
{
 [self .view endEditing:YES];
}

Hope it help You

kirti Chavda
  • 3,029
  • 2
  • 17
  • 29