I am trying to figure out how to make the UITextFieldDelegate methods be called for my UITextField. Here is some code I used to create the controller and text field:
@interface FirstViewController : UIViewController <UITextFieldDelegate> {
UITextField *fieldNumeroAppoggi;
}
@property (nonatomic,strong) UITextField *fieldNumeroArgomenti;
fieldNumeroAppoggi = [[UITextField alloc] initWithFrame:CGRectMake(210, 40, 50, 20)];
fieldNumeroAppoggi.borderStyle = UITextBorderStyleRoundedRect;
fieldNumeroAppoggi.tag = 1;
[self.dettagli addSubview:fieldNumeroAppoggi];
In my controller, if I implement the delegate method
- (BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
it is never called. If I set fieldNumeroAppoggi.delegate = self; though, everything works fine. If I create other UITextField objects, do I have to set the delegate for each of them?
Assuming I understand you… yes, an object can be the delegate for multiple controls. If you need to do different things based on which control sent the message, you handle that in the delegate handler. You can either compare the sender to the specific object or use the
tagproperty to identify specific instances.Example: