如何在目标C中为文本字段设置选择器方法?

时间:2014-10-15 12:22:13

标签: ios objective-c validation uitextfield

我正在为我的应用编写一个注册视图控制器。我需要验证表单。我认为设置文本值更改的选择器方法应该适用于包含表单数据的不同文本字段。 我在google上看到了旧的问题和内容,这是我到目前为止所拥有的

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.passwordInput.secureTextEntry = YES;
    self.btnDone.enabled = NO; //Done button needs to be disabled until form is properly validated
    self.emailInput.delegate = self; //emailinput is the property attached to Email textfield of the form
    self.passwordInput.delegate = self;
    emailCheck = NO;
    passwordCheck = NO;
    [self.emailInput addTarget:self action:@selector(formValidation) forControlEvents:UIControlEventValueChanged];
    [self.passwordInput addTarget:self action:@selector(formValidation) forControlEvents:UIControlEventValueChanged];
    // Do any additional setup after loading the view from its nib.
}

-(void) formValidation {

    NSString *regex = @"[^@]+@[A-Za-z0-9.-]+\\.[A-Za-z]+";
    NSPredicate *emailPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if(self.passwordInput.text.length >7)
    {
        passwordCheck = YES;
    }
    if([emailPredicate evaluateWithObject:self.emailInput.text])
    {
        emailCheck = YES;
    }
    if (self.passwordInput.text.length<7 || ![emailPredicate evaluateWithObject:self.emailInput])
    {
    self.warningLabel.text = @"Please enter a valid email/at least 8 character password";
    }
    if(passwordCheck == YES && emailCheck ==YES)
    {
        self.btnDone.enabled = YES;//button is enabled
    } }

现在的问题是该事件没有被解雇。输入数据时没有任何反应。任何人都可以指导我在这里做错了什么吗? 附:我不太明白UITextFieldTextDidChangeNotification。如果有人可以为我提出替代解决方案或解释这个概念,那就太棒了 我刚试过forControlEvents:UIControlEventEditingChanged,该应用因崩溃错误导致无法执行正则表达式。**"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do regex matching on object <UITextField: 0x9ae8610;"**

3 个答案:

答案 0 :(得分:1)

设置文本字段的委托并使用以下方法

-(void)textFieldDidEndEditing:(UITextField *)textField
{
  [self validateForm:textField.text];
}

并将表单验证程序功能更改为

-(void) validateForm:(NSString *)inputString {

//validate inputString

}

答案 1 :(得分:1)

按钮提交单击表单,即使您可以检查下面的验证。

 - (IBAction)pushToSignConfirmationScreen:(id)sender;
 {
     NSString *emailString = txt_Email.text;// storing the entered email in a string.
   / / Regular expression to checl the email format.
     NSString *emailReg = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    NSPredicate *emailTest=[NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailReg];

     if ([txt_ContactName.text length] == 0 || [txt_Address.text length] == 0
       || [txt_DBAName.text length] == 0 || [txt_City.text length] == 0
       || [txt_Email.text length] == 0 || [txt_Phone.text length] == 0
      || [txt_State.text length] == 0)
      {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"All fields are required to begin setting up a merchant account." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
       [alert show];

      return;
   }

  else if(([emailTest evaluateWithObject:emailString]!=YES)||[emailString isEqualToString:@""])
  {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Enter your email in abc@example.com format." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];

    txt_Email.text = @"";
    [btn_next setImage:[UIImage imageNamed:@"tag_icon_bt_up1.png"] forState:UIControlStateNormal];
    return;
}
else if([txt_ZipCode.text length]!=5)
{
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please enter a valid zip code." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];

      txt_ZipCode.text = @"";
     return;
  }
  else if([txt_Phone.text length]!=10 )
  {
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please enter a       valid mobile number." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
      [alert show];

      txt_Phone.text = @"";
      return;
}

答案 2 :(得分:0)

首先设置文本字段的委托,然后检查方法: -

- (BOOL)textFieldShouldReturn:(UITextField *)textField.

如果textField相同,请调用formValidation方法。