【发布时间】:2016-11-26 13:36:17
【问题描述】:
我有弹出警报并使用 UITextField 代码获取文本,但是当我想获取文本字符串时,我会出现 nil 错误。我的代码在这里。我正在使用最新的 Xcode 和 Swift 3。
var tField: UITextField!
@IBAction func addcommentButtonTapped(sender: AnyObject) {
let alert = UIAlertController(title: "Write Comment", message: "", preferredStyle: UIAlertControllerStyle.alert)
alert.addTextField { (configurationTextField) in
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler:self.handleCancel))
alert.addAction(UIAlertAction(title: "Send", style: UIAlertActionStyle.default, handler:{ (UIAlertAction)in
print("Done !!")
let textField = self.tField.text // HERE GIVES ERROR nil = tField
if textField != nil {
print("Item : \(textField))")
}
}))
self.present(alert, animated: true, completion: {
print("completion block")
})
}
}
错误
致命错误:在展开可选值时意外发现 nil
错误行
let textField = self.tField.text // 这里给出错误 nil = tField
谢谢!
【问题讨论】:
-
你有没有给
self.tField分配过东西?
标签: ios uitextfield swift3