通过HTML iOS打开日期选择器

时间:2013-10-18 14:41:01

标签: javascript html objective-c ios6

我正在iOS中构建混合应用程序,我需要在HTML页面中的控件上单击已打开日期选择器,该控件已加载到我的webview中。以下是该特定控件和相关javascript方法的HTML:

<input name="date" id="creationDate" class=dateET type="date" onclick="onDateSelected();" readonly />

function onDateSelected() 
{
console.log('onDateSelected');
            var dateOn = document.getElementById('creationDate').value;
            document.location.href = "#mCrm#onDateSelected#mCrm#"+dateOn;

}

在目标C中:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

    NSString *requestString = [[request URL]absoluteString];
    if (!([requestString rangeOfString:@"onDateSelected"].location == NSNotFound))
    {
        [self onDateSelected:requestString];
        return FALSE;
    }
    else if (!([requestString rangeOfString:@"onAddCustomerBtnClick"].location == NSNotFound))
    {

    }



    return TRUE;
}


 -(void)onDateSelected:(NSString *)reqString
    {
        custDate = [[UIDatePicker alloc] init];
        custDate.datePickerMode = UIDatePickerModeDate;
        UIToolbar* toolbar = [[UIToolbar alloc] init];
        toolbar.barStyle = UIBarStyleDefault;
        [toolbar sizeToFit];

        //to make the done button aligned to the right
        UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];


        UIBarButtonItem* doneButton = nil;

    //    if(sender == self.mCdob)
            doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                          style:UIBarButtonItemStyleDone target:self
                                                         action:@selector(doneDateOfBirth:)];


        [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];


        //custom input view
    //    sender.inputView = datePickerContact;
    //    sender.inputAccessoryView = toolbar;
    }

问题是,在单击日期类型输入控件时,日期选择器未打开。欢迎任何帮助!!

1 个答案:

答案 0 :(得分:0)

实际上是愚蠢的错误..我忘了添加以下方法:

-(void)doneDateOfCreation:(id) sender
{
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateStyle:NSDateFormatterMediumStyle];
    //[df setDateFormat:@"d/MM/YYYY"];

    NSString* date  = [df stringFromDate:[custDate date]];

    NSDate * st = [df dateFromString:date];

    NSLog(@"date= %@",[df stringFromDate:st]);


    [custDate resignFirstResponder]; //hides the pickerView
    [custDate removeFromSuperview];
    [toolbar removeFromSuperview];


}

同时将[self.view addSubview:toolbar];添加到方法onDateSelected