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

Posted

技术标签:

【中文标题】验证 ios 中的文本字段并设置 FirstResponder【英文标题】:Validating textfields in ios and setting the FirstResponder 【发布时间】:2014-10-10 21:06:18 【问题描述】:

如何正确使用 [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;

【问题讨论】:

【参考方案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];
    


【讨论】:

【参考方案2】:
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];
    

^^^我帮你修好了

【讨论】:

以上是关于验证 ios 中的文本字段并设置 FirstResponder的主要内容,如果未能解决你的问题,请参考以下文章

在字段纯PHP退出时验证输入字段

获取IOS模拟器文本字段中的值

如何在 iOS 中为文本字段设置数字范围?

iOS自动在文本字段中添加连字符

如何在ios中的tableview单元格上的文本字段上返回文本字段上的键盘

TextField 中的 IOS8 文本在焦点上反弹