3

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;
}
4

2 回答 2

4

更新

我尝试了下面的代码,并且确实有效,但是出于某种原因,如果您更改 FirstResponder,文本将不会被写入。所以我修改了功能。你应该做这个:

- (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...
}

无论如何,当您不想删除内容时,这不起作用,但是如果您更改代码以检查更改范围,这很容易解决。

我做了一个示例项目,你可以在这里得到它:TextFieldTest


我就是这样做的,使用文本字段委托,您可以检查新插入的字符,如果达到最大值,则移动到下一个文本字段:

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... 
}

(对不起,如果不编译我只是直接写在这里)

祝你好运!

于 2013-07-09T01:44:41.917 回答
0

您可以为这些文本字段设置委托,并在委托方法中实现您的业务逻辑(将其中一个文本字段设置为新的第一响应者)。

于 2013-07-09T01:40:54.557 回答