限制iOS Phonegap App中原生日期选择器中的可选日期

时间:2014-09-19 22:57:36

标签: html ios cordova datepicker

我正在使用 Phonegap Build iOS 开发应用程序,并且我使用输入数据类型来调用本机日期选择器(没有问题)并选择一些日期,但我想限制可选日期。

我已尝试使用输入的minmax属性,但会被忽略。在iOS 6和iOS 7上测试。

这是我的代码:

<input id="fecha-buscar" type="date" min="2014-01-01" max="2014-12-31" value="2014-01-01">

我找到了this similar SO question,但没有回答。

任何想法如何解决这个问题?可以修复min的{​​{1}}和max属性或其他方式,但可以调用iOS的原生日期选择器

谢谢!

2 个答案:

答案 0 :(得分:0)

fwiw,使用原生的cordova(不是pg build),我以为我会分享这项工作。

在MainViewController.m中

- (void)viewDidLoad
{
         :
    /* add this somewhere */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_pickerViewWillBeShown:) name: UIKeyboardWillShowNotification object:nil];
         :
}

- (void)_pickerViewWillBeShown:(NSNotification*)aNotification {
    [self performSelector:@selector(_resetPickerViewBackgroundAfterDelay) withObject:nil afterDelay:0];
}

-(void)_resetPickerViewBackgroundAfterDelay
{
    UIPickerView *pickerView = nil;
    for (UIWindow *uiWindow in [[UIApplication sharedApplication] windows]) {
        for (UIView *uiView in [uiWindow subviews]) {
            pickerView = [self _findPickerView:uiView];
        }
    }

    if (pickerView){
        NSDate *now = [NSDate date];
        NSCalendar *calendar = [[NSCalendar alloc]    initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
        //set for today at 8 am
        [components setHour:8];
        NSDate *todayAtTime = [calendar dateFromComponents:components];
        //set max at now + 60 days
        NSDate *futureDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * 60];

//        [pickerView setBackgroundColor:[UIColor greenColor]];
        [pickerView.superview setValue:@"15" forKey:@"minuteInterval"];
        [pickerView.superview setValue:futureDate forKey:@"maximumDate"];
        [pickerView.superview setValue:todayAtTime forKey:@"minimumDate"];
    }
}

-(UIPickerView *) _findPickerView:(UIView *)uiView
{
        if ([uiView isKindOfClass:[UIPickerView class]] ){
            return (UIPickerView*) uiView;
        }

        if ([uiView subviews].count > 0) {
            for (UIView *subview in [uiView subviews]){
                UIPickerView* view = [self _findPickerView:subview];
                if (view)
                    return view;
            }
        }
        return nil;
}

使用iOS 6/7,Cordova 3.5,devices / sim

进行测试

答案 1 :(得分:0)

我刚刚为phonegap / cordova找到了一个datepicker插件,它有min&amp; amp;最大值。 https://github.com/sectore/phonegap3-ios-datepicker-plugin/tree/ios7