J'ai une feuille d'action et j'ouvre une alerte avec eux avec les codelins suivants:
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) {
}
}
Je veux maintenant stocker l'entrée du champ de texte dans l'alerte dans Firebase: p >
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)
}
Je ne reçois aucune donnée du champ textField.
Merci d'avance pour votre aide!
3 Réponses :
Vous avez besoin de
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)
}
Comme ce self.textFieldAlert = textField.text! va stocker une valeur initiale vide, et déplacer cette ligne
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)
}
haut de la fonction, vous pouvez donc y accéder dans l'action d'alerte
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
Vous pouvez également le faire en gardant une référence à le champ de texte de l'alerte
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])
}
Merci ça marche! Une dernière question: comment activer l'action par défaut uniquement si l'utilisateur a mis du texte?
définissez un délégué sur le champ de texte de référence et utilisez ce stackoverflow.com/questions/39538098/... , la clé est self.alertController.actions [0] .isEnabled = true / false
Vous pouvez obtenir votre textField depuis votre alertController en utilisant alert.textFields?.first.
Exemple: strong>
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) {
}
}
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])
}
}