【问题标题】:Set limit to UITextField in ObjectiveC在Objective C中设置TextField的限制
【发布时间】:2016-06-09 07:22:01
【问题描述】:

我正在使用 Objective C 创建登录屏幕,我想在其中实现对用户名(电子邮件)和密码的验证。如何以非常简单的方式实现它。

【问题讨论】:

    标签: ios xcode objective-c-runtime


    【解决方案1】:

    您始终可以使用 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;
                }
            }
        }
    

    编辑

    根据您的 cmets,您没有收到 textField 代表,所以这是您可以做的 :)

    在您的 ViewController 中,确认 UITextFieldDelegate 使用,

    YourViewController : UIViewController <UITextFieldDelegate>
    

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

    self.emailTextField.delegate = self;
    

    【讨论】:

    • 我试过了,但我无法连接到委托功能。
    【解决方案2】:

    如果您单击验证按钮,设置验证按钮操作并查看以下代码,首先检查两个文本字段是否为空,如果您提供文本然后再次检查其有效的电子邮件类型,则所有条件都为真,然后输入你的代码并运行,

    -(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];
        }
    }
    

    它成功地为我工作,希望它有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      相关资源
      最近更新 更多