0

I used UIDatePicker in my ViewController. it worked fine on iOS 8.x, but when I run it on iOS 9.x devices,it crashed.

I have tried to create a new project to use UIDatePicker in the same way and it worked fine on all iOS devices.

So, my question is whether my project setting was wrong or the way I uesd UIDatePicker was wrong.

The crash information is:

click to see the crash log

I hava set a breakpoint, it just crash at _datePicker.datePickerMode = UIDatePickerModeDate; on iOS 9.3, and worked fine on iOS 8.1.

- (UIDatePicker *)datePicker {
    if (!_datePicker) {
        NSDate *nowDate = [NSDate date];
        _datePicker = [[UIDatePicker alloc] init];
        _datePicker.datePickerMode = UIDatePickerModeDate;
        _datePicker.hidden = YES;
        _datePicker.backgroundColor = [UIColor whiteColor];
        [_datePicker addTarget:self action:@selector(datePickerChanged) forControlEvents:UIControlEventValueChanged];
    }
    return _datePicker;
}
iAhmed
  • 6,556
  • 2
  • 25
  • 31
suool
  • 1
  • 2

1 Answers1

0

Its because of iOS 9 Update as reference to this question that is already answered. update your code like below.

  _datePicker = [[UIDatePicker alloc] init];
  _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
  _datePicker.datePickerMode = UIDatePickerModeDate;

UIPickerView and UIDatePicker are now resizable and adaptive—previously, these views would enforce a default size even if you attempted to resize them. These views also now default to a width of 320 points on all devices, instead of to the device width on iPhone. Interfaces that rely on the old enforcement of the default size will likely look wrong when compiled for iOS 9. Any problems encountered can be resolved by fully constraining or sizing picker views to the desired size instead of relying on implicit behavior.

Community
  • 1
  • 1
Im Batman
  • 1,842
  • 1
  • 31
  • 48