0

我正在 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;
    }

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

4

1 回答 1

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

于 2013-10-21T07:35:22.520 回答