Skip to content

Instantly share code, notes, and snippets.

@crgg
Created January 20, 2020 19:01
Show Gist options
  • Save crgg/87420996b73f61c8cb7912d2afdbc355 to your computer and use it in GitHub Desktop.
Save crgg/87420996b73f61c8cb7912d2afdbc355 to your computer and use it in GitHub Desktop.
// Implement the addName IBAction
@IBAction func addName(_ sender: UIBarButtonItem) {
let alert = UIAlertController(title: "New Name",
message: "Add a new name",
preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save",
style: .default) {
[unowned self] action in
guard let textField = alert.textFields?.first,
let nameToSave = textField.text else {
return
}
self.names.append(nameToSave)
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel)
alert.addTextField()
alert.addAction(saveAction)
alert.addAction(cancelAction)
present(alert, animated: true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment