cordova datepicker插件在iOS8中不起作用:未设置UITableView dataSource

时间:2014-10-17 14:41:58

标签: uitableview cordova datepicker ios8 uidatepicker

更新到iOS8后,cordova datepicker插件无法正常工作。它崩溃了以下消息:

由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:' UITableView数据源未设置'

但是我没有在插件源代码中看到任何与UITableView相关的代码。我不知道,也许UIDatePicker中有一个内部UITableView?

这是代码(我删除了一些非相关代码以便于阅读):

- (UIPopoverController *)createPopover:(NSMutableDictionary *)options {

  UIView *datePickerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, pickerViewWidth, pickerViewHeight)];

  if(!self.datePicker){
    self.datePicker = [self createDatePicker:options frame:frame];
    [self.datePicker addTarget:self action:@selector(dateChangedAction:) forControlEvents:UIControlEventValueChanged];
  }

  NSDateFormatter *formatter = [self createISODateFormatter:@"yyyy-MM-dd'T'HH:mm:ss'Z'" timezone:[NSTimeZone defaultTimeZone]];
  NSString *dateString = [options objectForKey:@"date"];
  self.datePicker.date = [formatter dateFromString:dateString];// if remove this line, it works fine

  [datePickerView addSubview:self.datePicker];

  UIViewController *datePickerViewController = [[UIViewController alloc]init];
  datePickerViewController.view = datePickerView;

  UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:datePickerViewController];
  popover.delegate = self;
  [popover setPopoverContentSize:CGSizeMake(pickerViewWidth, pickerViewHeight) animated:NO];

  [popover presentPopoverFromRect:anchor inView:self.webView.superview  permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];   

  return popover;
}

这一行:

self.datePicker.date = [formatter dateFromString:dateString];

最终导致崩溃,并且通过调试,我发现dateString的值是:

2014-10-17T10:0:00Z

2014-10-17T20:0:00Z

它从第二次显示崩溃,第一次总是好的

你能告诉我一些线索吗?感谢

1 个答案:

答案 0 :(得分:5)

最后我发现了这个问题:

cordova DatePicker的源代码,重用UIDatePicker实例:

if(!self.datePicker){
    self.datePicker = [self createDatePicker];
    [self.datePicker addTarget];
}

并且,它在iOS7中运行良好,但会导致iOS8崩溃。

在iOS8中,不要在多UIView中共享UIDatePicker实例,所以我将代码修改为:

// in iOS8, UIDatePicker couldn't be shared in multi UIViews, it will cause crash. so   create new UIDatePicker instance every time
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {

    self.datePicker = [self createDatePicker:options frame:frame];
    [self.datePicker addTarget:self action:@selector(dateChangedAction:) forControlEvents:UIControlEventValueChanged];

}else{

    if(!self.datePicker){
        self.datePicker = [self createDatePicker:options frame:frame];
        [self.datePicker addTarget:self action:@selector(dateChangedAction:) forControlEvents:UIControlEventValueChanged];
    }
}

希望这可以帮助像我这样的人