我在两个按钮上创建了一个工具栏,并在ios7上运行,当我在ios8崩溃时运行:
Terminating app two to uncaught exception
‘UIViewControllerHierarchyInconsistency’,reason: ‘child view
controller: Should
Have parent view controller: but requested parent is: ‘
这是在ios7中静默工作的代码片段:
expiredPromoTextField.inputView = DatePicker; expiredPromoTextField.delegate = self; quantityPromoTextField.inputView = quantityPicker; quantityPromoTextField.delegate = self; // Create button to close the UIPickerView UIToolbar * mypickerToolbar = [[UIToolbar alloc] initWithFrame: CGRectMake (0,320,56)]; mypickerToolbar.barStyle = UIBarStyleBlackTranslucent; [mypickerToolbar sizeToFit]; NSMutableArray * barItems = [[NSMutableArray alloc] init]; UIBarButtonItem * CancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel target: self action:selector (cancelDoneClicked)]; [barItems addObject: CancelBtn]; UIBarButtonItem * FLEXspace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: self action: nil]; [barItems addObject: FLEXspace]; UIBarButtonItem * doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target: self action:selector (pickerDoneClicked :)]; [barItems addObject: doneBtn]; [mypickerToolbar setItems: barItems animated: YES]; [quantityPicker setShowsSelectionIndicator: YES]; expiredPromoTextField.inputAccessoryView = mypickerToolbar; quantityPromoTextField.inputAccessoryView = mypickerToolbar;
你知道我的意思是inputAccessoryView会崩溃的应用程序,我也问苹果的工程师,他们告诉我,这是测试版的一个问题,但现在与GM继续给同样的问题.
我该怎么办?
解决方法
我在iOS 8上也有同样的例外,现在修正为以下代码.
关键是,您不应该添加输入视图作为视图控制器视图的子视图.
(我不知道为什么iOS 7中的代码运行良好,在iOS 8中不再奏效)
之前(发生错误)
UITextField* someTF; View* customView; UIViewController *mainVC; [mainVC.view addSubview:customView]; someTF.inputView = customView;
之后(工作良好)
UITextField* someTF; View* customView; UIViewController *mainVC; // [mainVC.view addSubview:customView]; <-- delete this line someTF.inputView = customView;