如何在 Firebase 中存储警报文本?
Posted
技术标签:
【中文标题】如何在 Firebase 中存储警报文本?【英文标题】:How to store text of alert in Firebase? 【发布时间】:2019-01-02 10:21:10 【问题描述】:我有一个操作表,我用以下代码打开了一个警报:
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 的任何数据。
提前感谢您的帮助!
【问题讨论】:
【参考方案1】:你需要
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)
【讨论】:
感谢它的工作!最后一个问题:如何仅在用户输入一些文本时才启用默认操作? 设置一个代表到参考文本字段并使用这个***.com/questions/39538098/…,关键是self.alertController.actions[0].isEnabled = true / false
【参考方案2】:
您可以使用alert.textFields?.first
从您的alertController
获取您的textField
。
示例:
func alertBeitrageMelden(postId: String)
// Create the action buttons for the alert.
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
alert.addTextField (textField) in
textField.placeholder = ""
let defaultAction = UIAlertAction(title: "Melden", style: .default) (action) in
if let textFieldAlert = alert.textFields?.first?.text, textFieldAlert.count > 0
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason": textFieldAlert])
let cancelAction = UIAlertAction(title: "Abbrechen", style: .cancel) (action) in
// Respond to user selection of the action.
alert.addAction(cancelAction)
alert.addAction(defaultAction)
self.present(alert, animated: true)
【讨论】:
【参考方案3】:alert.addTextField textField in
这是警报的配置处理程序,您可以在其中更改将要添加的文本字段的属性。但是在这里设置一些文本变量没有任何意义,因为此时文本字段的text
是空的。
你的目标应该是在你需要的时候得到text
的文本字段,在这种情况下是UIAlertAction
的handler
因此,在声明其操作并添加文本字段之前创建警报
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
alert.addTextField textField in
textField.placeholder = ""
然后在默认操作中检查此文本字段的文本是否不为空,以及是否不将此文本保存到 Firebase
let defaultAction = UIAlertAction(title: "Melden", style: .default) _ in
if let text = alert.textFields?.first?.text, !text.isEmpty
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason": text])
【讨论】:
以上是关于如何在 Firebase 中存储警报文本?的主要内容,如果未能解决你的问题,请参考以下文章