使用 UITextField Popup Alertview 获取文本 Swift 3 fatal error
Getting text with UITextField Popup Alertview with Swift 3 fatal error
我有弹出式警报并使用 UITextField 代码获取文本,但是当我想要获取文本字符串时却出现 nil 错误。我的代码在这里。我正在使用 Xcode latest 和 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")
})
}
}
错误
fatal error: unexpectedly found nil while unwrapping an Optional value
错误行
let textField = self.tField.text // HERE GIVES ERROR nil = tField
谢谢!
您已将 alert
action
添加到 addTextField
闭包中,您还需要使用 alertController
的文本字段数组中的第一个文本字段,因此您需要更改你的代码是这样的
@IBAction func addcommentButtonTapped(sender: AnyObject) {
let alert = UIAlertController(title: "Write Comment", message: "", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler:self.handleCancel))
alert.addTextField { (configurationTextField) in
//configure here your textfield
configurationTextField.textColor = UIColor.greenColor()
}
alert.addAction(UIAlertAction(title: "Send", style: UIAlertActionStyle.default, handler:{ (UIAlertAction)in
print("Done !!")
if let textField = alert.textFields?.first {
print("Item : \(textField.text))")
}
}))
self.present(alert, animated: true, completion: {
print("completion block")
})
}
我有弹出式警报并使用 UITextField 代码获取文本,但是当我想要获取文本字符串时却出现 nil 错误。我的代码在这里。我正在使用 Xcode latest 和 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")
})
}
}
错误
fatal error: unexpectedly found nil while unwrapping an Optional value
错误行
let textField = self.tField.text // HERE GIVES ERROR nil = tField
谢谢!
您已将 alert
action
添加到 addTextField
闭包中,您还需要使用 alertController
的文本字段数组中的第一个文本字段,因此您需要更改你的代码是这样的
@IBAction func addcommentButtonTapped(sender: AnyObject) {
let alert = UIAlertController(title: "Write Comment", message: "", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler:self.handleCancel))
alert.addTextField { (configurationTextField) in
//configure here your textfield
configurationTextField.textColor = UIColor.greenColor()
}
alert.addAction(UIAlertAction(title: "Send", style: UIAlertActionStyle.default, handler:{ (UIAlertAction)in
print("Done !!")
if let textField = alert.textFields?.first {
print("Item : \(textField.text))")
}
}))
self.present(alert, animated: true, completion: {
print("completion block")
})
}