2015-09-11 48 views
0

我已經編程創建了UITextField。現在,當我嘗試獲得其beginningOfDocument資產時,它變成nilUITextField beginningOfDocument屬性返回零

代碼如下:

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 
+0

讓我看看你的代碼 – Ducky

+0

什麼是'textField.beginingOfDocument' - 不是UITextField屬性!?什麼是AlertView? addTextField是做什麼的? – luk2302

+0

啊,它是'UITextInput'的一個屬性... – luk2302

回答

0

如果你只是創建的UITextField,然後立即嘗試訪問beginningOfDocument財產,甚至嘗試添加後它以超級觀點,你將永遠得到一個uninitialized價值。

嘗試的UITextField成爲第一個響應後取回財產和看到的結果:-)

好運。

+0

謝謝....我有upvoted,但有不到15分積極upvote沒有得到提高。 –

+0

以下是接受答案的方法:http://stackoverflow.com/help/someone-answers – Ducky