【发布时间】:2018-05-15 17:35:05
【问题描述】:
我在一个单独的类中有这个函数:
public static func TextField(x:CGFloat=0, y:CGFloat=0, width: CGFloat=0, height: CGFloat=0, placeholder: String="", fontSize:CGFloat=15, fontColor: UIColor=UIColor.black, fontWeight:UIFont.Weight=UIFont.Weight.light ,passwordField: Bool = false) -> UITextField{
weak var delegate: UITextFieldDelegate?
let textfield = UITextField(frame: CGRect(x: x, y: y, width: width, height: height))
textfield.placeholder = placeholder
textfield.textColor = fontColor
textfield.font = UIFont.systemFont(ofSize: fontSize, weight: fontWeight)
textfield.isSecureTextEntry = passwordField
textfield.isUserInteractionEnabled = true
textfield.autocorrectionType = UITextAutocorrectionType.no
textfield.keyboardType = UIKeyboardType.default
textfield.contentVerticalAlignment = UIControlContentVerticalAlignment.center
textfield.delegate = delegate
return textfield
}
它正确地创建了UITextField,并具有我所期望的正确行为,我无法键入任何UITextFields。
另外,我尝试像这样添加一个单独的类:
class MyTextFieldDelegate : NSObject, UITextFieldDelegate{
func textFieldShouldReturn(textField: UITextField!) -> Bool{
textField.resignFirstResponder()
return true;
}
}
但是不工作。 另外,阅读this StackOverflow post
unowned(unsafe) var delegate: UITextFieldDelegate?
返回错误:
'unowned' 只能应用于类和类绑定协议类型, 不是'UITextFieldDelegate?'
我怎么不能在我的UITextfield 上写字?
【问题讨论】:
-
您确定只是将代理设置为 nil 吗?我以为你必须将文本字段委托设置为 vc。
标签: swift uitextfield swift4