[英]UIDatePicker NSRangeException crash iOS 11
我有以下代碼將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:]
關於什么會導致這次崩潰的任何指針? 謝謝
請遵循此代碼。它可以幫助您。
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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.