在不同 class 中创建 UITextField 不允许输入
Create UITextField in different class doesn't allow to type
我在单独的class:
中有这个功能
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:
class MyTextFieldDelegate : NSObject, UITextFieldDelegate{
func textFieldShouldReturn(textField: UITextField!) -> Bool{
textField.resignFirstResponder()
return true;
}
}
但是没有用。
另外,阅读
unowned(unsafe) var delegate: UITextFieldDelegate?
returns错误:
'unowned' may only be applied to class and class-bound protocol types,
not 'UITextFieldDelegate?'
我怎么不能在 UITextfield
上写字?
您需要创建一个带有强引用的静态 let 变量,以便在您的工厂函数中使用它(遵循 Swift 命名约定并调用它,例如 makeDefaultTextField),只是为了在您分配时更清楚您赋予的委托负责管理委托方法给您的 class:
static let myDelegate = MyTextFieldDelegate()
public static func makeDefaultTextField(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 {
let textfield = UITextField(frame: CGRect(x: x, y: y, width: width, height: height))
//....
textField.delegate = myDelegate
}
如果您想在 ViewController 中管理委托方法(强烈建议),您可以通过这种方式进行,例如:
class YourViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let txtField = YourClass.makeDefaultTextField()
txtField.delegate = self
}
}
extension YourViewController: UITextFieldDelegate {
//call here the methods
}
我在单独的class:
中有这个功能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:
class MyTextFieldDelegate : NSObject, UITextFieldDelegate{
func textFieldShouldReturn(textField: UITextField!) -> Bool{
textField.resignFirstResponder()
return true;
}
}
但是没有用。
另外,阅读
unowned(unsafe) var delegate: UITextFieldDelegate?
returns错误:
'unowned' may only be applied to class and class-bound protocol types, not 'UITextFieldDelegate?'
我怎么不能在 UITextfield
上写字?
您需要创建一个带有强引用的静态 let 变量,以便在您的工厂函数中使用它(遵循 Swift 命名约定并调用它,例如 makeDefaultTextField),只是为了在您分配时更清楚您赋予的委托负责管理委托方法给您的 class:
static let myDelegate = MyTextFieldDelegate()
public static func makeDefaultTextField(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 {
let textfield = UITextField(frame: CGRect(x: x, y: y, width: width, height: height))
//....
textField.delegate = myDelegate
}
如果您想在 ViewController 中管理委托方法(强烈建议),您可以通过这种方式进行,例如:
class YourViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let txtField = YourClass.makeDefaultTextField()
txtField.delegate = self
}
}
extension YourViewController: UITextFieldDelegate {
//call here the methods
}