I have a table view and I have an add button that when pressed alerts three text fields to pop up when you finished and press add the data goes into the table view and is stored in Firebase.
let composeAlert = UIAlertController(title: "Add Employee", message: "Add Employee", preferredStyle: .alert)
composeAlert.addTextField { (textField:UITextField) in
textField.placeholder = "Name"
}
composeAlert.addTextField { (textField:UITextField) in
textField.placeholder = "Adress"
}
composeAlert.addTextField { (textField:UITextField) in
textField.placeholder = "Phone Number"
composeAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
composeAlert.addAction(UIAlertAction(title: "Add Employee", style: .default, handler: { (action:UIAlertAction) in
if let name = composeAlert.textFields?.first?.text,
let adress = composeAlert.textFields?.last?.text,
let documentID = composeAlert.textFields?.first?.text,
let phoneNumber = composeAlert.textFields?.last?.text
{
let newEmployee = Employee(name: name, adress: adress, phoneNumber: phoneNumber,documentID: documentID, timeStamp: Date())
var ref:DocumentReference? = nil
ref = self.db.collection("employee").addDocument(data: newEmployee.dictionary) { [self]
error in
if let error = error {
print("Error adding document: \(error.localizedDescription)")
}else{
loadData()
checkForUpdates()
print("Document added with ID: \(ref!.documentID)")
}
}
}
}))
}
self.present(composeAlert, animated: true, completion: nil)
}
This is the code for the alert pop-up. There are four composeAlert.textFields?.last?.text, and there is only room for .first and .last what do I do with the phoneNumber and the documentID?
I think You forgot to add the fourth textField, so after you add the the textfields you can access them like this
composeAlert.textFields?[0].text // first textfield name
composeAlert.textFields?[1].text // second textfield address
composeAlert.textFields?[2].text // third textfield documentID
composeAlert.textFields?[3].text // fourth textfield phoneNumber
and so on, but you have to make sure not to access a textfield with index number greater than the composeAlert textfields count
and here is the Final code
let composeAlert = UIAlertController(title: "Add Employee", message: "Add Employee", preferredStyle: .alert)
composeAlert.addTextField { (textField:UITextField) in
textField.placeholder = "Name"
}
composeAlert.addTextField { (textField:UITextField) in
textField.placeholder = "Adress"
}
composeAlert.addTextField { (textField:UITextField) in
textField.placeholder = "Phone Number"
}
composeAlert.addTextField { (textField:UITextField) in
textField.placeholder = "Document id"
}
composeAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
composeAlert.addAction(UIAlertAction(title: "Add Employee", style: .default, handler: { (action:UIAlertAction) in
if let name = composeAlert.textFields?[0].text,
let adress = composeAlert.textFields?[1].text,
let documentID = composeAlert.textFields?[2].text,
let phoneNumber = composeAlert.textFields?[3].text
{
// TODO: Add you code here to call firebase
}
}))
self.present(composeAlert, animated: true, completion: nil)