我将UIDatePicker作为输入视图添加到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;


如何在不创建不可见视图的情况下关闭UIDatePicker

最佳答案

尝试这个

[_editingField resignFirstResponder];


编辑

对于dismissDate不被调用。
删除这段代码

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


并添加这个

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


您的回叫

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

关于iphone - 关闭UIDatePicker,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13856455/

10-11 19:47