将UITextField限制为表示一个月的2位数

时间:2015-10-21 15:46:46

标签: ios objective-c uitextfield

我创建了一个视图控制器,允许用户输入他们的信用卡信息以继续购买。一切都很好,除了讨厌的一年和一年。月格式化。更具体地说,月份需要用户只能输入2位数(这很容易用'shouldChangeCharactersInRange'方法)加上某种检查来限制UITextField中可以保留哪种数字。目前,用户可以在内部输入任意2位数字(在文本字段中看起来像这样:83或21等)但我只需要1-12的数字。

在查看堆栈溢出时,我确信有人做过类似的事情,但我找不到任何东西。有人能指出我正确的方向吗?

里面放什么?

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    switch (textField.tag)
    {
        case 3: //Month.
            //This allows to tap on a UITextField, even if it already contains 2 digits, and deleting these digits with the delete key with out being thrown out because of 'resignFirstResponder'.
            if (textField.text.length > 1 && !([string length] == 0 && range.length > 0))
            {
                [_textfieldMonth resignFirstResponder];
            }


            //End up with UITextField displaying only from 01 - 12.


            break;

        case 4: //Year.
            break;
    }
}

3 个答案:

答案 0 :(得分:0)

您可以检查if/else条件以检查输入[yourTextField.text intValue] < 13 && [yourTextField.text intValue] > 0,如果它大于12或小于0,则发出alertview

if (textField.text.length > 1 && !([string length] == 0 && range.length > 0))
 {
      if([textField.text intValue]<13 &&[textField.text intValue]>0)
      {
        [_textfieldMonth resignFirstResponder];
      }
      else
      {
         UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please make sure you enter a value between 1-12" delegate:nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
         [message show];
      }
 }

除此之外,如果您需要进一步将用户输入转换为月份名称,如下所示:

int monthNumber = textField.text; 
NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSString *monthName = [[df monthSymbols] objectAtIndex:(monthNumber-1)];

答案 1 :(得分:0)

问题中代码的问题(以及其他答案中的当前代码)是在最近输入/粘贴/剪切/复制的文本实际发送到文本字段之前调用shouldChangeCharactersInRange 。您需要查看新文本的内容并验证它。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    switch (textField.tag)
    {
        case 3: //Month.
        {
            NSString *value = [textField.text stringByReplacingCharactersInRange:range withString:string];
            int month = [value intValue];
            if (month >= 1 && month <= 12)
            {
                [textField resignFirstResponder];
            }
            break;
        }
        case 4: //Year.
            break;
    }
}

更好的解决方案是等待用户实际尝试离开月份字段时验证月份条目,并防止它们在值无效时离开。

答案 2 :(得分:0)

这就是我所做的,它最终满足了我的需求。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    switch (textField.tag)
    {
        case 3: //Month.
            if (textField.text.length > 1 && !([string length] == 0 && range.length > 0))
            {
                int intValue = [textField.text intValue];
                if (intValue < 13)
                {
                    [_textfieldMonth resignFirstResponder];
                }
                else
                {
                    textField.text = [NSString stringWithFormat:@"Month"];
                    [_textfieldMonth resignFirstResponder];

                    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Error" message:@"This field only accepts numbers from 01 to 12" delegate:self  cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
                    [message show];
                }
            }
            break;
        case 4: //Year.
            break;
    }
}