简体   繁体   中英

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). Within the NameCellView.xib view controller there is a UITextField Named "NameField". Now i have tried to set up the UITextField's (NameField) delegate to be the file owner (eg the class of the cell = 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 :

    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 . I think the problem is in xib connections

If you're open to alternative ways to dismiss the keyboard, you can add a 'Done' button that when tapped, calls a "hideKeyboard" method.

You add this button to a keyboard accessory view (basically a simple UIToolBar with some customisation to make it look nice).

I don't know how to do this with Interface Builder but I do know how to do this with purely code:

// 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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