Why won't text fields respond to hiding the keyboard?
-
10-12-2019 - |
Question
This is my code:
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[txtSiteDesc resignFirstResponder];
[txtDesc resignFirstResponder];
[ssFS resignFirstResponder];
return YES;
}
This is the .h file:
#import <UIKit/UIKit.h>
@interface slEnterDataViewController : UITableViewController <UITextFieldDelegate> {
UITextField *txtSiteDesc;
UITextField *txtDesc;
UITextField *ssFS;
}
@property (nonatomic, retain) IBOutlet UITextField *txtSiteDesc;
@property (nonatomic, retain) IBOutlet UITextField *txtDesc;
@property (nonatomic, retain) IBOutlet UITextField *ssFS;
@end
It works for the txtSiteDesc, but not any of the others. I assume the problem is in the textFieldShouldReturn method; I thought I could check the value of "textField" to see which field it is and then execute the appropriate "resignFirstResponder" and return. I'm close (I think) but not close enough.
Help would be greatly appreciated. :D
Solution
Is it possible that you've set the delegate for txtSiteDesc
but not for your other two text fields? This would explain why textFieldShouldReturn:
is called for your first text field not not the others. Make sure you are setting the delegate
property of all three textfields to self
(where self = your view controller).
OTHER TIPS
Even I faced this problem. This is what you are looking for :
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string hasSuffix:@"\n"])
{
[theTextField resignFirstResponder];
return NO;
}
return YES;
}
This will hide the keyboard when you press return/done etc. on the keyboard.