【问题标题】:Change UITextField background when editing begins for multiple fields开始编辑多个字段时更改 UITextField 背景
【发布时间】:2013-05-12 08:38:43
【问题描述】:

所以我不久前找到了这个帖子: Change UITextField background when editing begins

最佳答案是在用户编辑它时更改 textField 的背景图像的绝佳资源。但是,我的问题是,如何使用 MULTIPLE 文本字段启用此功能?

特别是(对于我的应用程序)我有一个登录名和密码字段。使用我将在下面发布的代码,我可以让我的第一个文本字段在用户点击它时正确更改图像。但是,当用户点击下一个文本字段或键盘上的“下一个”选项时,我无法让下一个字段效仿。由于“textFieldShouldBeginEditing”和结尾只能有一个实例,因此您需要在同一部分下为两个字段设置图像更改代码。但是,当我这样做时,我的两个字段都会在第一个字段被点击时发生变化,并且在第二个字段被点击时都恢复。

对这个优秀的社区有什么想法吗?

这里有一些代码:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    _userNameTextField.background = [UIImage imageNamed:@"login_field_highlighted@2x"];
    return YES;
    _passwordTextField.background = [UIImage imageNamed:@"password_field_highlighted@2x"];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    _userNameTextField.background = [UIImage imageNamed:@"login_field@2x"];
    return YES;
    _passwordTextField.background = [UIImage imageNamed:@"password_field@2x"];
    return YES;
}

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"textFieldShouldReturn");
    if (textField == _userNameTextField) {
        [_passwordTextField becomeFirstResponder];
    } else if (textField == _passwordTextField) {
        [_passwordTextField resignFirstResponder];
    }

    return YES;

}

【问题讨论】:

    标签: objective-c uitextfield first-responder


    【解决方案1】:

    在每个方法中,您只需要检查目标文本字段,就像您在 textFieldShouldReturn 中所做的那样:

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
        if (textField == _userNameTextField) {
            _userNameTextField.background = [UIImage imageNamed:@"login_field@2x"];
        } else if (textField == _passwordTextField) {
            _passwordTextField.background = [UIImage imageNamed:@"password_field@2x];
        }
        return YES;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多