开发者

Why is setDelegate not working for UITextFieldDelegate, but working via Interface Builder

开发者 https://www.devze.com 2023-03-18 22:42 出处:网络
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, bu

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消