Created
December 11, 2017 03:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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