The first step is to declare an alertController as a variable.
let alertController = UIAlertController(title: "Alert Title", message: "Additional Information - message", preferredStyle: .alert)
The next step described an optional textField for user data entry.
alertController.addTextField { (textfield: UITextField) -> Void in }
Here we do something with textFiled data, adding it to an array. This would only be included to act on the textfield data.
let addAction = UIAlertAction(title: "Add", style: .default) { (action: UIAlertAction) in let textField = alertController.textFields?.first self.dataArray.append((textField?.text)!) self.tableView.reloadData() // reload the data in the tableview }
We can add additional buttons, here OK and Cancel, to remove the alert and perform optional actions as needed. Here, none for OK and print for Cancel.
let OKAction = UIAlertAction(title: "OK", style: .default) let cancelAction = UIAlertAction(title: "Cancel", style: .default) { (action: UIAlertAction) in print("Optional action") }
The final step is to add the actions to the alert viewcontroller and display the alert.
alertController.addAction(addAction) alertController.addAction(OKAction) alertController.addAction(cancelAction) present(alertController, animated: true, completion: nil)