在iOS 8中向numpad键盘添加“完成”按钮时出错

时间:2014-11-05 06:22:29

标签: objective-c iphone ipad ios8 xcode6

因此,小键盘键盘并没有“完成”键盘。按钮默认情况下,我想添加一个。在iOS 7及更低版本中,有一些技巧可以在键盘上添加一个按钮,但它们似乎不适用于iOS 8。

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if(textField.keyboardType == UIKeyboardTypeNumberPad){
        textField.inputAccessoryView=self.toolBar; ///self.toolBar is a IBOutlet of UIToolBar and in this toolBar have a button 'Done'.

}

运行此代码时,应用程序将终止并提供错误:

'UIViewControllerHierarchyInconsistency', reason: 'child view controller:
<UICompatibilityInputViewController: 0x7b283d20> should have parent view controller:
<HJ_PCreateUpdateCareTeamVC: 0x7a79bfc0> but requested parent is:
<UIInputWindowController: 0x7b9c9200>' 

1 个答案:

答案 0 :(得分:2)

我做了:

 textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 44)];
    textField.backgroundColor = [UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1.0];

[self.view addSubview:textField];
textField.keyboardType = UIKeyboardTypeNumberPad;


UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *doItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doDone)];
[toolBar setItems:@[doItem]];


textField.inputAccessoryView = toolBar;

///没关系