簡體   English   中英

如何單步執行多個UITextField

[英]How to step through multiple UITextFields

我有四個UITextField,我想在UIAlertView中使用,目前這就像它看起來像

在此輸入圖像描述

我希望能夠做到的是每個字段將每個框限制為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;
}

更新

我嘗試了下面的代碼,並且確實有效,但出於某種原因,如果您更改了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... 
}

(對不起,如果沒有編譯我只是直接寫在這里)

祝好運!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM