Zoomzoom
Zoomzoom

Reputation: 189

Why is my UITextField delegate method not called?

Here's my problem. I created a UITextField.

.h file:

@property (strong, nonatomic) UITextField *emailTextField;

.m file:

- (void)viewDidLoad
{
  [super viewDidLoad];
  self.emailTextField.delegate=self;
  [self setTextFields:120 :@"  email" :self.emailTextField];
}

-(void)setTextFields:(float)ycoord :(NSString *)text :(UITextField *)textField
{
  CGRect frame = CGRectMake(60, ycoord, 180, 30);
  textField = [[UITextField alloc]initWithFrame:frame];
  textField.backgroundColor = [UIColor whiteColor];
  textField.placeholder = text;
  textField.font = [UIFont fontWithName:@"Baskerville" size:15];
  textField.allowsEditingTextAttributes=YES;
  textField.autocorrectionType = UITextAutocorrectionTypeNo;

  CALayer *lay = [textField layer];
  [lay setCornerRadius:5.0f];
  [self.view addSubview:textField];
}

The aim is to save the text that the user put in the TextField. I tried with the delegate method

-(void)textFieldDidEndEditing:(UITextField *)textField

but the method is not called (I put a NSLog to check). Could someone help?

Upvotes: 0

Views: 6959

Answers (4)

Moose
Moose

Reputation: 2737

Probably the way InterfaceBuilder builds the view.

Try a [MyTextFieldDelegate new] at application launch. Your class will then be registered and correctly binded to the fields when your view is awaked from NIB.

Upvotes: 0

santhu
santhu

Reputation: 4792

- (void)viewDidLoad
 {
  [super viewDidLoad];

  self.emailTextField= [self setTextFields:120 :@"  email"];
  self.emailTextField.delegate=self;

  self.passwordTextField= [self setTextFields:200 :@"  password"];
  self.passwordTextField.delegate=self;

  self.nameTextField= [self setTextFields:250 :@"  name"];
  self.nameTextField.delegate=self;
 }

-(UITextField *)setTextFields:(float)ycoord :(NSString *)text{

  CGRect frame = CGRectMake(60, ycoord, 180, 30);

  UITextField* textField = [[UITextField alloc]initWithFrame:frame];
  textField.backgroundColor = [UIColor whiteColor];
  textField.placeholder = text;
  textField.font = [UIFont fontWithName:@"Baskerville" size:15];
  textField.allowsEditingTextAttributes=YES;
  textField.autocorrectionType = UITextAutocorrectionTypeNo;

  CALayer *lay = [textField layer];
  [lay setCornerRadius:5.0f];
  [self.view addSubview:textField];
  return textField;
  }

Upvotes: 5

Gavin
Gavin

Reputation: 8200

There are a couple issues:

You're setting the delegate before you actually create the UITextField.

When you create a UITextField in your setTextFields: method, you're never assigning it to your property. All you're doing is passing nil in to your method, and when you assign a value to the variable in your method, you're only assigning it within the context of that method. It doesn't go back and set the property.

Upvotes: 0

Stephen Johnson
Stephen Johnson

Reputation: 5121

  1. You set the delegate for emailTextField but when you create a UITextField you set textField. This could work if you @synthesize emailTextField to textField.

  2. Assuming that you are doing an @synthesize between emailTextFiled and textField or you change emailTextField to just textField, you are setting the delegate before you have created the object. You need to move setting the delegate until after you called setTextFields:

Upvotes: 0

Related Questions