2013-07-09 41 views
3

我有四个UITextFields,我想在UIAlertView中使用步骤,目前这是它看起来像如何通过多个UITextFields

enter image description here

我想做什么就能做的是一旦达到4个字符的限制,每个字段限制每个字段为4个字符,然后我希望下一个UITextField成为第一个响应者。

我也想能够做到这一点反向,所以如果字符被删除,一旦没有在第二场可用字符到第一,并开始删除等

目前我的代码是非常马虎,因为我只是想得到像这样的工作..所以这是它看起来像迄今为止。

//提示用户输入注册在这里 UIAlertView中*警报= [[UIAlertView中页头] initWithTitle:@ “请注册设备” 消息:@ “ ”代表:无cancelButtonTitle:@“ OK” otherButtonTitles:无]。

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 [email protected]"AAAA"; 
myTextField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; 
myTextField.textAlignment = NSTextAlignmentCenter; 
myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 

myTextField1.placeholder [email protected]"AAAA"; 
myTextField1.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; 
myTextField1.textAlignment = NSTextAlignmentCenter; 
myTextField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 

myTextField2.placeholder [email protected]"AAAA"; 
myTextField2.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; 
myTextField2.textAlignment = NSTextAlignmentCenter; 
myTextField2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 

myTextField3.placeholder [email protected]"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]; 

任何帮助将不胜感激。

// UPDATE: 这是我想使用的委托方法

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

更新

我尝试下面的代码,并没有工作,但如果你改变了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... 
} 

任何方式,当你wan't删除内容,这并不工作,但很容易,如果你改变了代码来检查的变化范围内解决。

我做了一个样本项目,你可以在这里: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... 
} 

(很抱歉,如果这么想的编译我只是写在这里直接)

祝您好运!

+0

好吧我会尽力处理这个..只是有一个问题与我的shouldChangeCharactersInRange委托方法不被调用,即使我已经将UITextFieldDelegate添加到标题中,并在viewDidLoad方法中设置委托.. – HurkNburkS

+0

这很奇怪,可以你也分享那个代码? – Raspu

+0

我用示例代码更新了我的问题,底部显示了我如何使用文本字段委托。 – HurkNburkS

0

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