0

如果出现错误,我想验证 UITextField 并显示 UIAlertView。

以前我使用下面的代码。它与 2 个 UIExfields 完美配合,但对于许多 UITextfield,这是不可能的。

任何提示如何显示 UIAlertView 来验证我的 UIExfields ?

- (void)processFieldEntries {

    NSString *usernameString = usernameField.text;
    NSString *passwordString = passwordField.text;
    NSString *firstNameString = firstNameField.text;
    NSString *nameString = nameField.text;
    NSString *cityString = cityField.text;

    NSString *errorText = @"Enter";
    NSString *usernameBlankText = @" a username";
    NSString *passwordBlankText = @" a password";
    NSString *prenomBlankText = @" a firstname";
    NSString *nomBlankText = @" a name";
    NSString *joinText = @", ";

    BOOL textError = NO;

    if (identifiantString.length == 0 || motDePasseString.length == 0 || prenomString.length == 0 || nomString.length == 0 || idPoubelle.length == 0) {
        textError = YES;

        if (usernameString.length == 0) {
            errorText = [errorText stringByAppendingString:usernameBlankText];
        }

        if (usernameString.length == 0 || passwordString.length == 0) {
            if (usernameString.length == 0) { 
                errorText = [errorText stringByAppendingString:joinText];
            }
            errorText = [errorText stringByAppendingString:passwordBlankText];
        }
    } 

    if (textError) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:errorText message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alertView show];
        return;
    }
}
4

2 回答 2

0

我会做这样的事情:

- (void)processFieldEntries {
    NSString *usernameString = usernameField.text;
    NSString *passwordString = passwordField.text;
    NSString *firstNameString = firstNameField.text;
    NSString *nameString = nameField.text;
    NSString *cityString = cityField.text;

    NSMutableArray *errors = [NSMutableArray array];

    if (usernameString.length == 0) {
        [errors addObject:@"a username"];
    }

    if (passwordString.length == 0) {
        [errors addObject:@"a password"];
    }

    if (firstNameString.length == 0) {
        [errors addObject:@"a first name"];
    }

    if (nameString.length == 0) {
        [errors addObject:@"a name"];
    }

    if (errors.count) {
        NSString *message = [NSString stringWithFormat:@"Enter %@", [errors componentsJoinedByString:@", "]];

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Errors" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }
}

当然,这并不能很好地扩展。考虑创建一组文本字段和错误消息。然后您可以迭代数组而不是编写一长串if语句。

于 2014-07-23T19:07:08.130 回答
0

您还可以在用户输入信息时进行一些额外的检查,因为例如名称不应包含数字或字母以外的其他符号。

一个不错的方法是从 View Controller 类实现 UITextFieldDelegate 协议。

然后为每个 UITextField 对象设置 tag 属性。

该协议有这个有用的方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

每次您在任何文本框中计时时都会调用它。

然后你可以检查:

switch (textfield.tag)
{
 case TEXT_FIELD_FOR_FIRST_NAME_TAG:
 {
  if (!validateFirstNameFunction) { /* Display some error */ }

  break;
 }

 case TEXT_FIELD_FOR_LAST_NAME_TAG:
 {
  if (!validateLastNameFunction) { /* Display some error */ }

  break;
 }

 // And so on...

}

尝试分离功能,这将使代码更清晰,更具可读性。

希望它也能给你带来一些价值:)

于 2014-07-23T23:38:34.647 回答