在 Objective C 中对 TextField 设置限制

Set limit to UITextField in ObjectiveC

我正在使用 Objective C 创建登录屏幕,我想在其中对用户名(电子邮件)和 password.How 实施验证以非常简单的方式实施。

您始终可以使用 textField shouldChangeCharactersInRange 委托来处理 textField 中允许的字符数。看看下面提供的解决方案 :) 希望对您有所帮助

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        if(textField == self.emailTextField){
            if (textField.text.length < 30 || string.length == 0){ 
                return YES;
            }
            else{
                return NO;
            }
        }
    }

编辑

根据您的评论,您没有收到 textField 委托,所以您可以执行以下操作:)

在您的 ViewController 中,确认 UITextFieldDelegate 使用

YourViewController : UIViewController <UITextFieldDelegate>

在您的 viewDidLoad 或 ViewWillAppear 中将 textField 委托设置为 self。

self.emailTextField.delegate = self;

如果你点击验证按钮,设置验证按钮动作,看下面的代码,首先检查两个文本字段是否为空,如果你输入文本然后再次检查其有效的电子邮件类型,那么所有条件都是是的,然后把你的代码和 运行,

-(IBAction)action:(id)sender
{
    if (tfMail.text.length == 0 || tfPass.text.length == 0) 
    {
         [self validatetextfield];
    }
    else if (![tfMail.text isEqualToString:@""])
    {

         NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
         NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
                //Valid email address
         if ([emailTest evaluateWithObject:tfMail.text] == YES)
         {
              //Its validated put your success code,
         }
         else
         {
              UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"Test!" message:@"Please Enter Valid Email Address. \nex. fdsjfkd@mail.com" preferredStyle:UIAlertControllerStyleAlert];
              UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
              }];

              [aler addAction:action];

              [self presentViewController:aler animated:YES completion:nil];
                    //not valid email address
          }
    }
}

警报消息方法:

-(void) validatetextfield
{
    if (tfMail.text.length==0) {
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"Email Field Empty!" message:@"Please Enter the Email" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];

        [aler addAction:action];

        [self presentViewController:aler animated:YES completion:nil];
        [tfMail becomeFirstResponder];
    }
    else if (tfPass.text.length==0)
    {
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"Password Field Empty!" message:@"Please Enter the Password" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
           [self dismissViewControllerAnimated:YES completion:nil];
        }];

        [aler addAction:action];

        [self presentViewController:aler animated:YES completion:nil];
        [tfPass becomeFirstResponder];
    }
}

它对我很成功,希望对你有帮助。