Keyboard won't close with UITextField

2019-08-20 11:50发布

问题:

In my code I have:

-(BOOL)textFieldShouldReturn:(UITextField*)theTextField{

if (theTextField == ITload)
{
    [ITload resignFirstResponder];
    return  YES;
}

if (theTextField == FacilitiesLoad) {
    [FacilitiesLoad resignFirstResponder];
    return YES;
}
return NO;
}

ITload and FacilitiesLoad are both my text fields. I'm using Numerical with punctuation keyboard and no done key appears. I have a return key which doesnt close the keyboard down.

Any ideas on how to display a done key and to get textFieldShouldReturn working please?

回答1:

It seems that you haven't set the delegate of your text fields to your receiver.

ITLoad.delegate = self;
FacilitiesLoad.delegate = self;

EDIT: you're getting warnings because your view controller doesn't (formally) comforms to the UITextFieldDelegate protocol. Declare it like this:

@interface MyViewController: UIViewController <UITextFieldDelegate> {
}

etc.