验证ios中的文本字段并设置First Responder

时间:2014-10-10 21:06:19

标签: ios ios7 ios6

如何正确使用[textfield becomeFirstResponder]。这里如果三个字段为空,我想将我的第一个文本字段设置为第一响应者,如果第一个和第三个字段为空则想要将第二个文本字段设置为焦点。我怎样才能有效地做到这一点。

这是我的代码

BOOL validName = [self validateUserField:mUsername];
BOOL validEmail = [self validateEmail:mEmail];
BOOL validPhone = [self validateUserField:mPhone];

NSString *errLbl = [NSString stringWithFormat:@"Followings error occured %@",@""];
if (!validName) {
    errLbl = [errLbl stringByAppendingFormat:@"%@",@"\n* valid name required"];
    [self.uErrorLabel setText:errLbl];
}

if (!validEmail) {
    errLbl = [errLbl stringByAppendingFormat:@"%@",@"\n* valid email required"];
    [self.uErrorLabel setText:errLbl];
    [self.uEmail becomeFirstResponder];
}

if (!validPhone) {
    errLbl = [errLbl stringByAppendingFormat:@"%@",@"\n* valid phone required"];
    [self.uErrorLabel setText:errLbl];
    [self.uPhone becomeFirstResponder];
}

if (!validName || !validEmail || !validPhone ) {

    if (!validName) {
        [self.uName becomeFirstResponder];
    }
    if (!validEmail) {
        [self.uEmail becomeFirstResponder];
    }
    if (!validPhone) {
        [self.uPhone becomeFirstResponder];
    }

    if(!validName && !validEmail && !validPhone){

        if(!validName && !validEmail){
            [self.uName becomeFirstResponder];
        }
        if(!validEmail && !validPhone){
            [self.uEmail becomeFirstResponder];
        }
        if(!validName && !validPhone){
            [self.uName becomeFirstResponder];
        }
    }

    return;
}

2 个答案:

答案 0 :(得分:1)

你只能激活其中一个文本字段,所以我会使用一个变量来跟踪哪一个被激活 -

    BOOL validName = [self validateUserField:mUsername];
    BOOL validEmail = [self validateEmail:mEmail];
    BOOL validPhone = [self validateUserField:mPhone];

    UITextField *activateTextField=nil;

    NSMutableString *errLbl = [[NSMutableString new] appendString:@"The following error(s) occurred"];

    if (!validName) {
        [errLbl appendString:@"\n* valid name required"];
        activateTextField=self.uName;
    }

    if (!validEmail) {
        [errLbl appendString:@"\n* valid email required"];
        activateTextField= activateTextField ? : self.uEmail;
    }

    if (!validPhone) {
        [errLbl appendString:@"\n* valid phone required"];
        activateTextField = activateTextField ? :self.uPhone;
    }

    if (activateTextField) {
        self.uErrorLabel.text=errLbl;
        [activateTextField becomeFirstResponder];
    }

}

答案 1 :(得分:0)

bool error = false

NSString *errLbl = @"The following errors occurred:";
if (![self validateUserField:mUsername]) {
    errLbl = [errLbl stringByAppendingString:@"\n* valid name required"];
    error = true;
}

if (![self validateEmail:mEmail]) {
    errLbl = [errLbl stringByAppendingString:@"\n* valid email required"];
    error = true;
}

if (![self validateUserField:mPhone]) {
    errLbl = [errLbl stringByAppendingString:@"\n* valid phone required"];
    error = true;
}

if (error) {
    [self.uErrorLabel setText:errLbl];

    if (!validName) {
        [self.uName becomeFirstResponder];
    }
    else if (!validEmail) {
        [self.uEmail becomeFirstResponder];
    }
    else if (!validPhone) {
        [self.uPhone becomeFirstResponder];
    }
}

^^^我为你修好了