将输入视图添加到文本字段 iOS 8 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24029113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:21:23  来源:igfitidea点击:

Error when adding input view to textfield iOS 8

iosobjective-cios8

提问by Milo

I'm having an issue when I add a custom input view to my app on iOS 8. This was working perfectly fine on iOS 7 but when switching to iOS 8 everything fails.

当我在 iOS 8 上向我的应用程序添加自定义输入视图时遇到问题。这在 iOS 7 上工作得很好,但是当切换到 iOS 8 时,一切都失败了。

This is the stack trace:

这是堆栈跟踪:

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) 

The relevant part being the top few lines. Can someone explain this? All I'm calling is myTextview.inputView = keyboard; Keyboard being a UIView created in the storyboard and linked via IBOutlet.

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

回答by Tuyen Nguyen

I encountered this problem before.

我以前遇到过这个问题。

Problem:

问题

  • You have to ensure that the view you will assign to inputViewor inputAccessoryViewdoesn't belong to any parent view. When you create these views from a xib inside a ViewController, by default they are set as subviews of a superview.
  • 您必须确保您将分配给的视图inputViewinputAccessoryView不属于任何父视图。当您从 ViewController 内的 xib 创建这些视图时,默认情况下它们被设置为超级视图的子视图。

Solution Tips:

解决方法提示

  • Use the method removeFromSuperviewon the view you will assign to inputViewor inputAccessoryView
  • 使用removeFromSuperview您将分配给的视图的方法inputViewinputAccessoryView

回答by Rudi

Just before becomeFirstResponderremove the View you use for input from the superview :

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

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

Hope it helps.

希望能帮助到你。

回答by pkamb

You have to ensure that the view you will assign to inputViewor inputAccessoryViewdon't belong to any parent view.

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

This error is especially annoying as any "input view" created in a storyboard (and thus added to the VC's view) cannot be set as the inputViewor inputAccessoryViewwithout causing this crash.

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

If the input view is not added to the VC's view, the input view will not be available for visual editing in the Interface Builder Storyboard. It's only visible in the Storyboard's left pane.

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

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

如何将 Xcode Storyboard 的“模拟指标”工具栏连接到实际的 IBOutlet UIToolbar?

I'd prefer to make an IB Connection to the inputAccessoryViewdirectly in the Storyboard. That causes this crash. A solution I found is to make a secondary IBOutlet connected to the view in the Storyboard, then in viewDidLoadremove it from the super view then immediately assign to inputAccessoryView. Not sure if I'll end up using this.

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

- (void)viewDidLoad {

    // ...

    [self.keybordView removeFromSuperview];
    self.inputAccessoryView = self.keybordView;
}

回答by Milo

I figured it out. I'm not sure what the issue was before but instead of linking a UIViewin interface builder, I instead created a UIViewprogrammatically and it worked fine.

我想到了。我不确定之前的问题是什么,但我没有UIView在界面构建器中链接一个,而是以UIView编程方式创建了一个,它运行良好。

回答by Octav

My problem was that I have named a view having a textField 'inputAccessoryView'...

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

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

Error:

错误:

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>'

Solution:

解决方案:

Check the naming; do not override if not needed.

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

回答by Lucio Fonseca

I found the solution creating the UIView programmatically (Like suggested by Milo), but it crashes when its used like a @property. In my case, I needed to create the UIPickerView instance in viewDidLoad and assing to UITextView inputView. When I need to acess the pickerView I get it like this

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

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

回答by Sasho

Yeah, the Tuyen Nguyen solution did not work for me. What worked for me in iOS 8 / Xcode 6 for setting an inputAccessoryView was not to removeFromSuperviewbut instead:

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

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

I hope this helps someone.

我希望这可以帮助别人。

回答by J. Lopes

I had the same problem when I tried to setup my UIDatePicker in my UITextField

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

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

My solution, I just "alloc" and "init" my datePicker in ViewDidLoad

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

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

回答by aryaxt

Calling removeFromSuperViewin viewDidLoaddoesn't seem to work properly in modals. I ended up setting the view hidden, and then removing it from superView in viewDidAppear.

调用removeFromSuperViewviewDidLoad似乎没有在模态正常工作。我最终将视图设置为隐藏,然后将其从viewDidAppear.

回答by Adam Mendoza

Most answers I was were related to code changes. I found out that deleting an outlet to the UIView inside the main view fixed it. https://stackoverflow.com/a/40037236/464313

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