【问题标题】:Error when adding input view to textfield iOS 8将输入视图添加到文本字段 iOS 8 时出错
【发布时间】:2014-07-24 14:41:24
【问题描述】:

当我在 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) 

相关部分是前几行。有人可以解释一下吗?我要打的电话是myTextview.inputView = keyboard;键盘是在情节提要中创建并通过 IBOutlet 链接的 UIView。

【问题讨论】:

    标签: ios objective-c ios8


    【解决方案1】:

    是的,Tuyen Nguyen 解决方案对我不起作用。 在 iOS 8 / Xcode 6 中设置 inputAccessoryView 对我有用的不是removeFromSuperview,而是:

    [[UIApplication sharedApplication].keyWindow addSubview:**MyViewToBeTheUIAccessoryView**];
    

    我希望这对某人有所帮助。

    【讨论】:

      【解决方案2】:

      您必须确保分配给inputViewinputAccessoryView 的视图不属于任何父视图。

      这个错误特别烦人,因为在故事板中创建的任何“输入视图”(并因此添加到 VC 的 view)都无法设置为 inputViewinputAccessoryView 而不会导致此崩溃。

      如果输入视图未添加到 VC 的 view,则输入视图将无法在 Interface Builder Storyboard 中进行可视化编辑。它仅在情节提要的左侧窗格中可见。

      How to connect Xcode Storyboard's "Simulated Metrics" toolbar to an actual IBOutlet UIToolbar?

      我更愿意直接在情节提要中建立与inputAccessoryView 的 IB 连接。这导致了这次崩溃。我发现的一个解决方案是在 Storyboard 中创建一个连接到视图的辅助 IBOutlet,然后在 viewDidLoad 中将其从超级视图中删除,然后立即分配给 inputAccessoryView。不确定我是否会最终使用它。

      - (void)viewDidLoad {
      
          // ...
      
          [self.keybordView removeFromSuperview];
          self.inputAccessoryView = self.keybordView;
      }
      

      【讨论】:

      • 非常感谢! “您必须确保分配给 inputView 或 inputAccessoryView 的视图不属于任何父视图。”最终解决了我一直在努力解决的问题。
      • inputAccessoryView 是 getOnly 属性。
      【解决方案3】:

      我的问题是我命名了一个具有 textField 'inputAccessoryView' 的视图...

      @property (weak, nonatomic) IBOutlet CustomView *inputAccessoryView;
      

      错误:

      Terminating app due to uncaught exception
      'UIViewControllerHierarchyInconsistency', reason: 'child view
      controller:<UICompatibilityInputViewController: 0x7fed7063a1a0> should
      have parent view controller:<MyViewController: 0x7fed70606f00> but
      requested parent is:<UIInputWindowController: 0x7fed710cde00>'
      

      解决方案:

      检查命名;如果不需要,请不要覆盖。

      【讨论】:

        【解决方案4】:

        当我从情节提要返回任何视图为inputAccessoryView 时,我遇到了同样的错误。我用 childViewController 修复了它。看我的回答here

        【讨论】:

          【解决方案5】:

          我对这个问题的解决方案是将视图添加到 inputView,而不是将视图控制器添加到父级。

          【讨论】:

            【解决方案6】:

            我得到的大多数答案都与代码更改有关。我发现删除主视图内的 UIView 的出口修复了它。 https://stackoverflow.com/a/40037236/464313

            【讨论】:

              【解决方案7】:

              我以前遇到过这个问题。

              问题

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

              解决方案提示

              • 在您将分配给inputViewinputAccessoryView 的视图上使用方法removeFromSuperview

              【讨论】:

              • 对我来说,我将它们添加为子视图并将它们设置为 inputView/inputAccessoryView。这似乎在 IOS7 中运行良好。在 IOS8 中,我删除了 addSubviews 来修复这个错误。
              • @Miro,你没看清楚这个问题。这个问题只发生在 iOS8 上。 iOS8之前,一切正常。
              • 这对我不起作用,但我在下面的回答对我有帮助。
              • 在 Swift 和 viewDidLoad 方法中:添加 self.myToolbar.removeFromSuperview()
              • 众所周知,在 13.4.1 上使用 inputAccessoryView 与另一个父级一起工作,但在 12.4 上崩溃
              【解决方案8】:

              我找到了以编程方式创建 UIView 的解决方案(就像 Milo 建议的那样),但是当它像 @property 一样使用时它会崩溃。在我的情况下,我需要在 viewDidLoad 中创建 UIPickerView 实例并分配给 UITextView inputView。当我需要访问 pickerView 时,我会像这样得到它

              UIPickerView *picker = (UIPickerView *)self.myTextField.inputView;
              

              【讨论】:

                【解决方案9】:

                viewDidLoad 中调用 removeFromSuperView 在模态中似乎无法正常工作。我最终将视图设置为隐藏,然后将其从 viewDidAppear 的 superView 中删除。

                【讨论】:

                  【解决方案10】:

                  当我尝试在我的 UITextField 中设置我的 UIDatePicker 时遇到了同样的问题

                  - (void)setupViews {
                      ...
                      dobField.inputView = aDatePicker; // Here was the problem
                      ...
                  }
                  

                  我的解决方案,我只是在 ViewDidLoad 中“分配”和“初始化”我的 datePicker

                  - (void)viewDidLoad
                  {
                      [super viewDidLoad];
                      // Do any additional setup after loading the view.
                      ...
                      aDatePicker = [[UIDatePicker alloc] init]; // My solution
                      ...
                  }
                  

                  【讨论】:

                    【解决方案11】:

                    就在becomeFirstResponder 之前,从超级视图中删除您用于输入的视图:

                    mTextField.inputView = mInputVeiw;
                    [mInputVeiw removeFromSuperview];
                    [mTextField becomeFirstResponder];
                    

                    希望对你有帮助。

                    【讨论】:

                      【解决方案12】:

                      我想通了。我不确定之前的问题是什么,但我没有在界面生成器中链接UIView,而是以编程方式创建了UIView,它工作正常。

                      【讨论】:

                      • 您能详细说明一下吗?有一个UITextField 和一个inputView 设置为UIPickerViewUIView 在哪里发挥作用?
                      • @CliftonLabrum 没有 UIPickerView
                      • @CliftonLabrum 以编程方式执行此操作:1. 创建一个选择器 2. 创建一个空的UITextField 3. 设置UITextField.inputView = picker 4. 添加[self.viewcontroller addSubview: myUITextField]
                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2019-08-05
                      • 2010-10-24
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多