UIDatePicker NSRangeException崩溃iOS 11

时间:2017-10-26 22:36:25

标签: ios objective-c uidatepicker

我有以下代码将DatePicker添加到我的一个UIViews中。

UIDatePicker *datePicker =
      [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 30, 320, 250)];
  [datePicker setDatePickerMode:UIDatePickerModeDate];
  datePicker.hidden = NO;
  datePicker.date = [NSDate date];
  [datePicker addTarget:self
                 action:@selector(changeDateInLabel:)
       forControlEvents:UIControlEventValueChanged];
  [self.dateView addSubview:datePicker];

此代码已存在一段时间,但我们现在注意到一些随机崩溃。崩溃只发生在iOS 11中。这就是崩溃的堆栈跟踪看起来像

Fatal Exception: NSRangeException
    *** -[__NSArrayM objectAtIndex:]: index 9223372036854775807 beyond bounds [0 .. 1]    
    Fatal Exception: NSRangeException
        0  CoreFoundation                     0x180d87d38 __exceptionPreprocess
        1  libobjc.A.dylib                    0x18029c528 objc_exception_throw
        2  CoreFoundation                     0x180d20c44 _CFArgv
        3  CoreFoundation                     0x180c50cc0 -[__NSArrayM removeObjectAtIndex:]
        4  UIKit                              0x18a3ecaa8 -[UIPickerView selectedRowInComponent:]
        5  UIKit                              0x18ac52224 -[_UIDatePickerMode_Date _dateForYearRow:]
        6  UIKit                              0x18ac4edd8 -[_UIDatePickerMode dateForRow:inCalendarUnit:]
        7  UIKit                              0x18ac4fa70 -[_UIDatePickerMode _updateSelectedDateComponentsWithNewValueInComponent:usingSelectionBarValue:]
        8  UIKit                              0x18ac4fd18 -[_UIDatePickerMode selectedDateComponents]
        9  UIKit                              0x18ac43370 -[_UIDatePickerView _updatedLastSelectedComponentsByValidatingSelectedDateWithLastManipulatedComponent:]
        10 UIKit                              0x18ac427e8 -[_UIDatePickerView _setDate:animated:forced:]
        11 UIKit                              0x18ac42d24 -[_UIDatePickerView _setMode:]
        12 UIKit                              0x18ac42e40 -[_UIDatePickerView setDatePickerMode:]

关于什么会导致这次崩溃的指示? 谢谢

1 个答案:

答案 0 :(得分:-2)

请遵循此代码。它可以帮助您。

UIDatePicker *datePicker=[[UIDatePicker alloc]init];
    datePicker.datePickerMode=UIDatePickerModeDate;
    [self.dateSelectionTextField setInputView:datePicker];
    UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    [toolBar setTintColor:[UIColor grayColor]];
    UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowSelectedDate)];
    UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
    [self.dateSelectionTextField setInputAccessoryView:toolBar];.

-(void)ShowSelectedDate
{   NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd/MMM/YYYY hh:min a"];
    self.dateSelectionTextField.text=[NSString stringWithFormat:@"%@",[formatter stringFromDate:datePicker.date]];
    [self.dateSelectionTextField resignFirstResponder];
}