作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个操作表,我使用以下 codelins 向他们打开警报:
func showActionSheet(postId: String) {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let action = UIAlertAction(title: NSLocalizedString("Beitrag melden", comment: ""), style: .default, handler: { _ in
self.alertBeitrageMelden(postId: postId)
})
actionSheet.addAction(action)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
我现在想将文本字段的输入存储在 Firebase 的警报中:
func alertBeitrageMelden(postId: String){
// Create the action buttons for the alert.
let defaultAction = UIAlertAction(title: "Melden", style: .default) { (action) in
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason": self.textFieldAlert])
}
let cancelAction = UIAlertAction(title: "Abbrechen", style: .cancel) { (action) in
// Respond to user selection of the action.
}
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = ""
if textField.text?.count ?? 0 > 0 {
self.textFieldAlert = textField.text!
}
}
alert.addAction(cancelAction)
alert.addAction(defaultAction)
self.present(alert, animated: true) {
}
}
我没有得到 textField 的任何数据。
预先感谢您的帮助!
最佳答案
你需要
if let te = alert.textFields?.first?.text , te.count > 0 {
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason":te])
}
因为这个 self.textFieldAlert = textField.text!
将存储一个空的初始值,并移动这一行
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
函数顶部,因此您可以在警报操作中访问它
func alertBeitrageMelden(postId: String) {
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Melden", style: .default) { (action) in
if let te = alert.textFields?.first?.text , te.count > 0 {
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason":te])
}
})
alert.addAction(UIAlertAction(title: "Abbrechen", style: .cancel) { (action) in
// Respond to user selection of the action.
})
alert.addTextField { (textField) in
textField.placeholder = ""
}
self.present(alert, animated: true)
}
您也可以通过保留对警报文本字段的引用来做到这一点
var alertTexf:UITextField!
func alertBeitrageMelden(postId: String){
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Melden", style: .default) { (action) in
if let te = self.alertTexf.text , te.count > 0 {
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason":te])
}
})
alert.addAction(UIAlertAction(title: "Abbrechen", style: .cancel) { (action) in
// Respond to user selection of the action.
})
alert.addTextField { (textField) in
textField.placeholder = ""
self.alertTexf = textField
}
self.present(alert, animated: true)
}
关于swift - 如何在 Firebase 中存储警报文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54004579/
我是一名优秀的程序员,十分优秀!