简体   繁体   English

为UITextField设置委托时,应用崩溃

[英]App crashes when setting delegate for UITextField

I ave an app n which a table view instantiates cell objects from a class called "NameCell" that has a nib file called "NameCellView.xib" (Name Cell is the class for NameCellView). 我有一个应用程序n,表视图从名为“ NameCell”的类实例化单元格对象,该类具有一个名为“ NameCellView.xib”的笔尖文件(Name Cell是NameCellView的类)。 Within the NameCellView.xib view controller there is a UITextField Named "NameField". 在NameCellView.xib视图控制器中,有一个名为“ NameField”的UITextField。 Now i have tried to set up the UITextField's (NameField) delegate to be the file owner (eg the class of the cell = NameCell). 现在,我尝试将UITextField的(NameField)委托设置为文件所有者(例如,单元格的类= NameCell)。 But when I do that, the app crashes as soon as I try interacting with the textfield (eg when I tap it), and the only message I get is '(lldb)' and the following line highlighted in green in the main.m app file : 但是当我这样做时,应用程序会在尝试与文本字段进行交互时崩溃(例如,当我点击它时),并且我收到的唯一消息是“(lldb)”,并且在main.m中以绿色突出显示以下行应用文件:

    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

Any ideas as for why that happens and how to fix it? 关于为什么会发生以及如何解决的任何想法? All I am trying to do id dismiss the keyboard when the user taps the 'return' key, so if you have a better way to do that without delegation I am all ears! 我试图做的所有操作都是在用户点击“返回”键时关闭键盘,因此,如果您有更好的方法来进行此操作而无需委派,我将不胜感激!

Thanks a lot for your help! 非常感谢你的帮助! Any comment is highly appreciated! 任何意见,高度赞赏!

By the description the things are not clear. 通过描述,事情还不清楚。 But please check the connections in xib . 但是请检查xib中的连接。 I think the problem is in xib connections 我认为问题出在xib连接中

If you're open to alternative ways to dismiss the keyboard, you can add a 'Done' button that when tapped, calls a "hideKeyboard" method. 如果您愿意使用其他方法来关闭键盘,则可以添加一个“完成”按钮,在点击该按钮时调用“ hideKeyboard”方法。

You add this button to a keyboard accessory view (basically a simple UIToolBar with some customisation to make it look nice). 您可以将此按钮添加到键盘附件视图(基本上是一个简单的UIToolBar,并进行了一些自定义以使其看起来不错)。

I don't know how to do this with Interface Builder but I do know how to do this with purely code: 我不知道如何使用Interface Builder做到这一点,但我确实知道如何使用纯代码来做到这一点:

// setup done button accessory view for keyboard

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolBar setBarStyle:UIBarStyleBlackTranslucent];

UIButton *btnDone = [[UIButton alloc] initWithFrame:CGRectMake(20, 8, 65, 30)];
[btnDone setTitle:@"Done" forState:UIControlStateNormal];
btnDone.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:14];
btnDone.backgroundColor = [UIColor darkGrayColor];
btnDone.layer.cornerRadius = 5.0;
btnDone.tintColor = [UIColor darkGrayColor];
[btnDone addTarget:self action:@selector(hideKeyboard) forControlEvents:UIControlEventTouchUpInside];

[toolBar addSubview:btnDone];

[btnDone release];

// -------------------------------------------------------------------
// let your text field's keyboard know about the accessory view
// -------------------------------------------------------------------
myTextField.inputAccessoryView = toolBar;

...

// Hide Keyboard Method
-(void)hideKeyboard
{
    [myTextField resignFirstResponder];
}

This will add a custom view above the keyboard so you can press the 'Done' button to hide the keyboard. 这将在键盘上方添加一个自定义视图,因此您可以按“完成”按钮隐藏键盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM