我想做的是在 UITextField 中做一个自定义清除按钮,目前除了最后一部分外,我已经完成了所有工作。这是让它执行明确的行动。但是,我无法弄清楚如何清除它,只是它所在的文本字段。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[textField setTextColor:[UIColor colorWithRed:0.56f green:0.56f blue:0.56f alpha:1.00f]];
[textField setFont:[UIFont systemFontOfSize:14]];
textField.background = [UIImage imageNamed:@"whiteCell"];
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:@selector(clearText:) forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(0, 0, 25, 25);
[btnColor setBackgroundImage:[UIImage imageNamed:@"clearBut"] forState:UIControlStateNormal];
textField.rightView = btnColor;
textField.rightViewMode = UITextFieldViewModeAlways;
[textField addSubview:btnColor];
return YES;
}
这就是我在 textField 中创建按钮的方式,并且它只在编辑时显示,正如你所看到的,我有它在调用,clearText
但是我不确定如何发送我所在的当前 textField 名称以被清除clearText
通话。
我知道我可以通过艰难的方式做到这一点,并为我的每个文本字段单独定义它,但我确信有一种更简单的方法可以解决这个问题,我只是还没有意识到。