Adding UIDatePicker as an input element for a text field in iOS 8

I have a UIDatePicker in my storyboard view associated with an IBOutlet in the header file.

In the implementation file, I set some properties in the collector and then assigned it to my text fields:

[self.txtEndDate setInputView:self.picker]; 

This works fine in iOS 7, but with iOS 8 it gives 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?

+6
source share
3 answers

After receiving the email from Apple Technical Support, it seems that to add the UIDatePicker (or any custom keyboard for what I understood) to the viewController you no longer need to add it to your view, but you will add it to your title bar and then connect it to IBOutlet.

It works for me, even if it doesn’t work in the iPhone 5 simulator (everything else is fine), and I went crazy.

I hope this can help other people with the same problem.

+6
source

The solution is to create the UIPickerView code in the code (remove it from the storyboard), assign it to the inputView text box and get it from there anytime you need it (instead of referencing it). This basically 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 need it later, use:

 UIPickerView* pickerView = (UIPickerView*) self.datePartySizeTextField.inputView; [pickerView selectRow:1 inComponent:0 animated:NO]; 
+2
source

UIDatePicker should not be a child of a superview

Problem

You must make sure that the view you assign to inputView or inputAccessoryView does not belong to any parent view. Maybe when you create these views from xib inside the ViewController, by default they are areas of the add-in.

Tips for solving :

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

see details in this link

Error adding input type to iOS 8 text box

+1
source

Source: https://habr.com/ru/post/975581/


All Articles