如何单步执行多个UITextField

时间:2013-07-09 01:31:38

标签: iphone ios objective-c uitextfield uialertview

我想在UIAlertView中使用四个UITextField,目前这就是它的样子

enter image description here

我希望能够做到的是每当达到4个字符限制时,每个字段限制为4个字符,然后我希望下一个UITextField成为第一个响应者。

我还希望能够反向执行此操作,如果在第二个字段中没有可用字符时删除字符,请转到第一个字段并开始删除等。

目前我的代码非常草率,因为我只是想尝试这样的工作......所以这就是它到目前为止的样子。

//提示用户在此处输入注册     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@“Please Register Device”消息:@“”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];

非常感谢任何帮助。

//更新:  这就是我尝试使用委托方法

的方法
//.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 个答案:

答案 0 :(得分: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


我就是这样做的,使用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... 
}

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

祝你好运!

答案 1 :(得分:0)

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