6

I have an UIDatePicker in my storyboard view connected to an IBOutlet in the header file.

In the implementation file I set some properties to the picker and then assign it to my textFields:

[self.txtEndDate setInputView:self.picker];

This was working fine in iOS 7, but with iOS 8 it's giving me the following error:

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x7c2d8800> should have parent view controller:<InserimentoDurata: 0x7aec2b10> but requested parent is:<UIInputWindowController: 0x7b92b400>'

Any idea on how to fix this?

Aleph72
  • 857
  • 1
  • 13
  • 35

3 Answers3

6

After receiving an email from Apple's Developer Technical Support, it seems that to add a UIDatePicker (or any custom keyboard for what I've understood) to a viewController, you don't have to add it to its view anymore, but you add it to its title bar and then connect it to the IBOutlet.

It's working for me, even if it doesn't work in the iPhone 5 simulator (all the others are ok) and I was going nuts.

I hope this could be of help for other people with the same problem.

Aleph72
  • 857
  • 1
  • 13
  • 35
2

The solution is to build your UIPickerView in code (remove it from the Storyboard), assign it to the textfield's inputView, and retrieve it from there anytime you need it (instead of keeping a reference to it). Basically, this means:

UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 300, 320, 168)];
[picker setDataSource: self];
[picker setDelegate: self];
picker.showsSelectionIndicator = YES;
self.textField.inputView = picker;

If you later need it, use:

UIPickerView* pickerView = (UIPickerView*) self.datePartySizeTextField.inputView;
[pickerView selectRow:1 inComponent:0 animated:NO];
verybadalloc
  • 5,588
  • 2
  • 29
  • 45
  • I tried to do it this way, but the date picker does not appear when the text fields become first responder. I don't understand why. – Aleph72 Sep 23 '14 at 09:14
  • Yep, it turns out this does not work anymore. It is the same problem as described here: http://stackoverflow.com/questions/24366437/add-uipickerview-in-uiactionsheet-from-ios-8-not-working – verybadalloc Sep 23 '14 at 22:47
  • This absolutely does work! I was not able to get anything to work until I found this answer! I did not implement the latter part of your answer though. Rather, I created a picker property (instead of ivar) and referred to it when needed. Here is my well documented code example. I hope this saves someone a lot of time and headache. https://stackoverflow.com/a/46047257/3634990 – jungledev Sep 05 '17 at 05:04
1

UIDatePicker should not be child of any super view

Problem:

You have to ensure that the view you will assign to inputView or inputAccessoryView don't belong to any parent view. Maybe when you create these views from xib inside a ViewController, by default they are subviews of a superview.

Solution Tips:

Using method removeFromSuperview for the view you will assign to inputView or inputAccessoryView

see detail in this link

Error when adding input view to textfield iOS 8

Community
  • 1
  • 1
umairhhhs
  • 400
  • 6
  • 18