2013-07-09 45 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

您可以爲這些文本字段設置委託,並實現業務邏輯(設置文本字段作爲新的一個第一響應者)在委託方法中。