Why is setDelegate not working for UITextFieldDelegate, but working via Interface Builder
I have the f开发者_运维知识库ollowing code below, and I can't figure out why the textFieldShouldReturn
method is not being called. When I use IB to create the connection to the delegate, it works, but when done programmatically, the log statement is not being displayed. What am I doing wrong here?
Secondly, in the textFieldShouldReturn
, some examples return YES
, and some return NO
. The ios documentation specified that returning YES
would provide default implementation. Can someone please explain this in more detail?
//.h
file
@interface GoSocietyLoginController : UIViewController <UITextFieldDelegate> {
}
- (IBAction)textFieldDoneEditing:(id)sender;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
@end
//.m
file
@interface GoSocietyLoginController ()
@property (nonatomic,retain) IBOutlet UITextField *login;
@property (nonatomic,retain) IBOutlet UITextField *password;
@end
@implementation GoSocietyLoginController
@synthesize login;
@synthesize password;
- (void)viewDidLoad
{
[super viewDidLoad];
[login setDelegate:self];
[password setDelegate:self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"Hello World");
if ([textField isEqual:login]) {
[password becomeFirstResponder];
}
return NO;
}
From the code that you've posted, it does not appear to me that you have declared the outlets in your header:
IBOutlet UITextField *login;
IBOutlet UITextField *password;
Be sure to declare them and set up the connections in the IB.
精彩评论