将输入视图添加到textfield iOS 8时出错

Mil*_*ilo 65 objective-c ios ios8

当我在iOS 8上为我的应用程序添加自定义输入视图时,我遇到了问题.这在iOS 7上完全正常,但是当切换到iOS 8时,一切都失败了.

这是堆栈跟踪:

2014-06-03 21:23:54.237 MyApp[1910:47245] *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x1103f9d40>
should have parent view controller:<StopChooser: 0x11083e200> but requested parent is:<UIInputWindowController: 0x110858800>'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001042eee35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000103b919a0 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001042eed6d +[NSException raise:format:] + 205
    3   UIKit                               0x00000001023d94cd -[UIViewController _addChildViewController:performHierarchyCheck:notifyWillMove:] + 184
    4   UIKit                               0x0000000102977a2b -[UIInputWindowController changeToInputViewSet:] + 416
    5   UIKit                               0x0000000102973f56 -[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:] + 185
    6   UIKit                               0x000000010297826a -[UIInputWindowController setInputViewSet:] + 526
    7   UIKit                               0x0000000102973c97 -[UIInputWindowController performOperations:withAnimationStyle:] + 50
    8   UIKit                               0x00000001027559bb -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1054
    9   UIKit                               0x0000000102422afd -[UIResponder becomeFirstResponder] + 468
    10  UIKit                               0x00000001023235d3 -[UIView(Hierarchy) becomeFirstResponder] + 99
    11  UIKit                               0x00000001029cdbfb -[UITextField becomeFirstResponder] + 51
    12  UIKit                               0x0000000102655d61 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 177
lib: terminating with uncaught exception of type NSException
    (lldb) 
Run Code Online (Sandbox Code Playgroud)

相关部分是前几行.有人可以解释一下吗?我所说的只是myTextview.inputView = keyboard; 键盘是在故事板中创建并通过IBOutlet链接的UIView.

Tuy*_*yen 112

我之前遇到过这个问题.

问题:

  • 您必须确保您将分配inputViewinputAccessoryView不属于任何父视图的视图.当您从ViewController中的xib创建这些视图时,默认情况下它们被设置为superview的子视图.

解决方案的提示:

  • removeFromSuperview在要分配给的视图上使用该方法inputViewinputAccessoryView

  • 对我来说,我是将它们添加为子视图并将它们设置为inputView/inputAccessoryView.这似乎在IOS7中运作良好.在IOS8中,我删除了addSubviews以修复此错误. (3认同)

Rud*_*udi 19

becomeFirstResponder删除您用于superview输入的View 之前:

mTextField.inputView = mInputVeiw;
[mInputVeiw removeFromSuperview];
[mTextField becomeFirstResponder];
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.