UIDatePicker NSRangeException crash iOS 11

吃可爱长大的小学妹 提交于 2019-12-21 12:14:46

问题


I have the following code to add a DatePicker to one of my 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];

This code has been around for a while but we've been noticing some random crashes now. The crashes only happen in iOS 11. This is what the stack trace of the crash looks like

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:]

Any pointers on what would be causing this crash? Thank you


回答1:


Follow this code.it is help you.

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];
}


来源:https://stackoverflow.com/questions/46964869/uidatepicker-nsrangeexception-crash-ios-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!