Search code examples
iosobjective-cuitextfielduitextfielddelegate

Checking UITextField.text from the delegate?


In my application, I have 3 UITextField declared in my header:

@property (strong, nonatomic) IBOutlet UITextField *email;
@property (strong, nonatomic) IBOutlet UITextField *username;
@property (strong, nonatomic) IBOutlet UITextField *password;

Once I have the values, I assign them to my variables and continue on with other account creation processes.

What I am trying to do is the following, as of right now, my UITextField "Tab" from one another using the .tag property declared in the delegate method as follows:

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
    NSInteger nextTag = textField.tag + 1;
    
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
    if (nextResponder) {
        
        [nextResponder becomeFirstResponder];
    } else {
        
        [textField resignFirstResponder];
    }
    return NO;
}

I have an additional alertView that contains a label that alerts the users for things such as:

-> Username already exists, Email already exists, Valid password must contain at least 5 characters, etc..

Instead of alerting the users that they have made any of the mistakes mentioned above when the click on the "sign up" button, i'd like of them to know of their mistakes as soon as the textfield resigns firstResponder and has at least 1 character in it's .text property.

Can someone please point me in the right direction, thanks!

UPDATE

I have attempted to implement the textFieldDidEndEditing delegate method:

-(BOOL)textFieldDidEndEditing:(UITextField*)textField {
    
    
    if (self.passwordTF.text.length <=4)
    {
        NSString *noValidPW = @"Passwords must be atleast 5 characters";
        NSMutableAttributedString *noValidPWAttrString = [[NSMutableAttributedString alloc] initWithString:noValidPW attributes:alertAttrs];
        self.alertLabel.attributedText = noValidPWAttrString;
        
        [UIView transitionWithView:[self redAlert]
                          duration:0.2
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:NULL
                        completion:NULL];
        
        self.redAlert.hidden = NO;
        
    }
    
    return NO;
}

But that is not working, that is giving me the password alert as soon as I stop editing ANY field, not just the password field.

Update This is the code that generates animates my alert :

NSString *noValidPW = @"Passwords must be atleast 5 characters";
    NSMutableAttributedString *noValidPWAttrString = [[NSMutableAttributedString alloc] initWithString:noValidPW attributes:alertAttrs];
    self.alertLabel.attributedText = noValidPWAttrString;

[UIView transitionWithView:[self redAlert]
                  duration:0.2
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:NULL
                completion:NULL];

self.redAlert.hidden = NO;

I have similar alerts for other error types (for other textfields), so its important to be able to check which textfield was the one that just ended being edited, does it have any characters on its .text property? If so, it needs to pass the validation code I provided right above this.


Solution

  • You can use -textFieldDidEndEditing: for that. Your missing piece was making sure that the textField that called the delegate method is the one you are interested in.

    E.g.:

    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
        // Check only if the user didn't leave the textfield blank
        if ([textField.text length] >= 1)
        {
           if (textField == self.password)
           {
                NSString *passwordText = textField.text;
                if ([passwordText length] < 5)
                {
                     [self showPasswordLengthError];
                }
                else if ( /* check other password stuff */ )
                {
                     [self showPasswordOtherError];
                }
           }
           else if (textField == self.username)
           {
                // Check username conditions and show errors accordingly
           }
           else if (textField == self.email)
           {
                // Check email stuff
           }
           // etc.
        }
    }
    
    - (void)showPasswordLengthError
    {
          NSString *noValidPW = @"Passwords must be at least 5 characters";
          NSMutableAttributedString *noValidPWAttrString = [[NSMutableAttributedString alloc] initWithString:noValidPW attributes:alertAttrs];
          self.alertLabel.attributedText = noValidPWAttrString;
    
          [UIView transitionWithView:[self redAlert]
                  duration:0.2
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:NULL
                completion:NULL];
    
          self.redAlert.hidden = NO;
    
         // Hide alertLabel after 5 sec
         [self performSelector:@selector(hideAlertLabel) withObject:nil afterDelay:5.0];
    }
    
    - (void)hideAlertLabel
    {
          self.redAlert.hidden = YES;
    }