我以编程方式创建了一个UITextField
。现在当我试着得到它的beginningOfDocument
属性时,它变成了nil
。
代码如下:
public func addTextField(title:String?=nil) -> UITextField {
kWindowHeight -= 80.0
let txt: UITextField = UITextField(frame: CGRect(x: 13, y: 67, width: 213.00, height: 35));
txt.addTarget(self, action: "textFieldDidChange", forControlEvents: UIControlEvents.EditingChanged)
txt.borderStyle = UITextBorderStyle.RoundedRect
txt.layer.cornerRadius = 3
txt.layer.borderColor = UIColorFromRGB(0x1E8ADB).CGColor
txt.font = UIFont(name: kDefaultFont, size: 14)
txt.autocapitalizationType = UITextAutocapitalizationType.Words
txt.clearButtonMode = UITextFieldViewMode.WhileEditing
txt.layer.masksToBounds = true
txt.layer.borderWidth = 1.0
if title != nil {
txt.placeholder = title!
}
contentView.addSubview(txt)
return txt
}
在另一个类中,我调用:
let textField = AlertView().addTextField("Send SMS")
var begining = textField.beginingOfDocument
如果您只是创建一个UITextField然后立即尝试访问beginningOfDocument
属性或甚至在尝试将其添加到父视图后,您将始终获得uninitialized
值。
尝试在UITextField成为第一个响应器后检索该属性并查看结果:-)
好运。