Reputation: 1721
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?
Upvotes: 0
Views: 1997
Reputation: 21967
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 tag
property to identify specific instances.
Example:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
BOOL shouldReturn = YES;
if (textField == fieldNumeroAppoggi) {
// do something
}
else if (textField == aDifferentTextField) {
// do something different
// maybe this one shouldn't return
shouldReturn = NO;
}
return shouldReturn;
}
Upvotes: 3