Atchoum Atchoum - 3 months ago 14
Objective-C Question

Dismiss UIDatePicker

I add a

UIDatePicker
as input view to
UITextField


UIDatePicker *oBirth;
NSDateFormatter *dateFormat;

_editingField = [[UITextField alloc] initWithFrame : CGRectMake(80, 12, 215, 25)];
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat : @"dd/MM/yyyy"];

oBirth = [[UIDatePicker alloc] initWithFrame : CGRectMake(0, 0, 120, 45)];
oBirth.date = [dateFormat dateFromString : sField];
NSLog(@"%@", oBirth.subviews);
[oBirth addTarget : self
action : @selector(updateDate:)
forControlEvents : UIControlEventValueChanged];
[oBirth addTarget : self
action : @selector(dismissDate:)
forControlEvents : UIControlEventTouchUpOutside];
[oBirth setDatePickerMode : UIDatePickerModeDate];
_editingField.tag = 1;
_editingField.inputView = oBirth;


How can I dismiss the
UIDatePicker
without creating an invisible view ?

Answer

Try this

[_editingField resignFirstResponder];

Edit

For dismissDate not being called.
Remove this piece of code

[oBirth addTarget : self
           action : @selector(dismissDate:)
 forControlEvents : UIControlEventTouchUpOutside];

And add this

     // single tap gesture recognizer
        UITapGestureRecognizer *tapGestureRecognize = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDate:)];
        tapGestureRecognize.numberOfTapsRequired = 1;
        [self.view addGestureRecognizer:tapGestureRecognize];


Your callback

- (void)dismissDate:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"Resign your responder");
}
Comments