3
votes

I have four UITextFields that I would like to use in a UIAlertView, Currently this is what it is looking like

enter image description here

What I would like to be able to do is restrict each box to 4 characters once each field once that 4 character limit is reached then I would like the next UITextField to become the first responder.

I would also like to be able to do this in reverse so if characters are being deleted once there are no characters available in the second field go to the first and start deleting etc.

Currently my code is pretty sloppy as I am just trying to get something like this working.. so this is what it looks like so far.

//prompt user to enter registration here UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Register Device" message:@" " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 53, 30)];

UITextField *myTextField1 = [[UITextField alloc] initWithFrame:CGRectMake(77, 45, 53, 30)];
UITextField *myTextField2 = [[UITextField alloc] initWithFrame:CGRectMake(142, 45, 53, 30)];
UITextField *myTextField3 = [[UITextField alloc] initWithFrame:CGRectMake(207, 45, 53, 30)];

// need to do something with first reasponder once 4 digits has been entered per box etc.
[myTextField becomeFirstResponder];

myTextField.placeholder =@"AAAA";
myTextField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField.textAlignment = NSTextAlignmentCenter;
myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

myTextField1.placeholder =@"AAAA";
myTextField1.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField1.textAlignment = NSTextAlignmentCenter;
myTextField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

myTextField2.placeholder =@"AAAA";
myTextField2.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField2.textAlignment = NSTextAlignmentCenter;
myTextField2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

myTextField3.placeholder =@"AAAA";
myTextField3.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField3.textAlignment = NSTextAlignmentCenter;
myTextField3.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

[myTextField setBackgroundColor:[UIColor whiteColor]];
[myTextField1 setBackgroundColor:[UIColor whiteColor]];
[myTextField2 setBackgroundColor:[UIColor whiteColor]];
[myTextField3 setBackgroundColor:[UIColor whiteColor]];

[alert addSubview:myTextField];
[alert addSubview:myTextField1];
[alert addSubview:myTextField2];
[alert addSubview:myTextField3];

[alert show];

Any help would be greatly appreciated.

// UPDATE: this is how I am trying to use the delegate method

//.h
<UITextFieldDelegate>

//.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//..
    [self.myTextField setDelegate:self];
    [self.myTextField1 setDelegate:self];
    [self.myTextField2 setDelegate:self];
    [self.myTextField3 setDelegate:self];
//..
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
     NSLog(@"yay");
    return NO;
}
2

2 Answers

4
votes

Update

I tried the code below, and does work, but for a reason if you change the FirstResponder, the text won't get written. So I modified the function. You should do this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField.text.length >= MAX_LENGTH)
        return NO;

    if((textField.text.length + string.length) >= MAX_LENGTH)
    {
        int currentTag = textField.tag;

        if(currentTag == 4)
            return YES;

        UITextField *newTextField = (UITextField *) [textField.superview viewWithTag:(currentTag+1)];
        [newTextField becomeFirstResponder];
        textField.text = [textField.text stringByAppendingString:string];
    }
    return YES; //you probably want to review this...
}

Any way, this doesn't work when you wan't to delete content, but that's easy solved if you change the code to check the range of the changes.

I made a sample project, you can get it here: TextFieldTest


This is how I would do it, using the textfield delegate, you can check the new inserted characters, if you reach the maximum, you move to the next textfield:

static int MAX_LENGTH = 4;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   if((textField.text.length + string.length) >= MAX_LENGTH)
   {
       //Get next TextField... A simple way to do this:
       UITextField *newTextField = [textField.superview viewWithTag:(textField.tag+1)];
       [newTextField becomeFirstResponder];
       //remember to set the tags in order
   }
   return YES; //you probably want to review this... 
}

(Sorry if dosen't compile I just wrote it here directly)

Good Luck!

0
votes

You can set delegate for these text fields and implement your business logic (set one of the text fields as the new first responder) in the delegate methods.