我正在为我的项目编写一个UIButton抽象类。主要逻辑是当文本字段为空时,在用户启动输入文本时禁用并启用该按钮。当用户删除所有文本时,它将再次被禁用。
现在我不知道如何将UITextfield Delegate放入我的UIButton抽象类中。我是iOS的新手,这是我第一次这样做,所以请有人帮我解决这个问题吗?非常感谢你。我会在下面附上我的代码:
class BaseEnterButton: UIButton, UITextFieldDelegate {
var originalButtonText: String?
var activityIndicator: UIActivityIndicatorView!
var buttonTitle:String?
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
commonInit()
}
func commonInit() {
}
func showLoading(){
originalButtonText = self.titleLabel?.text
self.setTitle("", for: .normal)
if (activityIndicator == nil) {
activityIndicator = createActivityIndicator()
}
showSpinning()
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.isEnabled = true
}
func hideLoading() {
let checkmarkImg = UIImage(named: "success")
self.setTitle(originalButtonText, for:.normal)
activityIndicator.stopAnimating()
self.setImage(checkmarkImg, for: .normal)
}
private func createActivityIndicator() -> UIActivityIndicatorView {
let activityIndicator = UIActivityIndicatorView()
activityIndicator.hidesWhenStopped = true
activityIndicator.color = UIColor.white
return activityIndicator
}
private func showSpinning() {
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(activityIndicator)
centerActivityIndicatorInButton()
activityIndicator.startAnimating()
}
答案 0 :(得分:-1)
使用以下方法添加UIButton
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// self.scrollView.frame = CGRectMake(0, 35, self.view.bounds.size.width,
self.view.bounds.size.height - 250);
[UIView commitAnimations];
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:@selector(btnColorPressed:)
forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(150, 5, 25, 25);
[btnColor setBackgroundImage:[UIImage
imageNamed:@"PaintPickerButton.png"] forState:UIControlStateNormal];
[self.txtField addSubview:btnColor];
return YES;
}
也许这会有所帮助