Skip to content

Instantly share code, notes, and snippets.

@Lancewer
Created December 11, 2017 03:39
Show Gist options
  • Save Lancewer/bc3440c0fcae64bf91f25f6fcf656a95 to your computer and use it in GitHub Desktop.
Save Lancewer/bc3440c0fcae64bf91f25f6fcf656a95 to your computer and use it in GitHub Desktop.
[Simple AlertController in iOS] how to create a basic alert controller with a textfield in iOS #iOS #Swift #alert #alertController #textField
@IBAction func addName(_ sender: Any) {
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, handler: nil)
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