UIAlertController is one of the most basic component of the app development, because using the UIAlertController to get the feedback…
original source : https://medium.com/swift-india/uialertcontroller-in-swift-22f3c5b1dd68
alert controller 사용 방법에 대해 간단 명료하게 설명
UIAlertController is one of the most basic component of the app development, because using the UIAlertController to get the feedback, confirmation, choose between the options from the user, also we do alerts.
Title : The title of the alert.
Alert Message : Descriptive text that provides more details about the reason for the alert.
Alert Style : Indicating the type of alert to display.
Alert Actions : The actions that the user can take in response to the alert or action sheet.
Alert TextField : Text field displayed by the alert.
Alert Styles: Alert Or Action Sheet?
When creating your alert controller, you can choose between two options from enum UIAlertControllerStyle
.alert : An alert displayed modally for the app.
.actionSheet : An action sheet displayed in the context of the view controller that presented it.
/** Simple Alert - Show alert with title and alert message and basic two actions */ func showSimpleAlert() { let alert = UIAlertController(title: "Sign out?", message: "You can always access your content by signing back in", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: { _ in //Cancel Action })) alert.addAction(UIAlertAction(title: "Sign out", style: UIAlertActionStyle.default, handler: {(_: UIAlertAction!) in //Sign out action })) self.present(alert, animated: true, completion: nil) }
/** Simple Action Sheet - Show action sheet with title and alert message and actions */ func showSimpleActionSheet(controller: UIViewController) { let alert = UIAlertController(title: "Title", message: "Please Select an Option", preferredStyle: .actionSheet) alert.addAction(UIAlertAction(title: "Approve", style: .default, handler: { (_) in print("User click Approve button") })) alert.addAction(UIAlertAction(title: "Edit", style: .default, handler: { (_) in print("User click Edit button") })) alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { (_) in print("User click Delete button") })) alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: { (_) in print("User click Dismiss button") })) self.present(alert, animated: true, completion: { print("completion block") }) }
Alert with distractive button
/** Simple Alert with Distructive button */ func showAlertWithDistructiveButton() { let alert = UIAlertController(title: "Sign out?", message: "You can always access your content by signing back in", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: { _ in //Cancel Action })) alert.addAction(UIAlertAction(title: "Sign out", style: UIAlertActionStyle.destructive, handler: {(_: UIAlertAction!) in //Sign out action })) self.present(alert, animated: true, completion: nil) }
Alert with more than 2 buttons
/** Simple Alert with more than 2 buttons */ func showAlertWithThreeButton() { let alert = UIAlertController(title: "Alert", message: "Alert with more than 2 buttons", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "Default", style: .default, handler: { (_) in print("You've pressed default") })) alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (_) in print("You've pressed cancel") })) alert.addAction(UIAlertAction(title: "Destructive", style: .destructive, handler: { (_) in print("You've pressed the destructive") })) self.present(alert, animated: true, completion: nil) }
/** Simple Alert with Text input */ func showAlertWithTextField() { let alertController = UIAlertController(title: "Add new tag", message: nil, preferredStyle: .alert) let confirmAction = UIAlertAction(title: "Add", style: .default) { (_) in if let txtField = alertController.textFields?.first, let text = txtField.text { // operations print("Text==>" + text) } } let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in } alertController.addTextField { (textField) in textField.placeholder = "Tag" } alertController.addAction(confirmAction) alertController.addAction(cancelAction) self.present(alertController, animated: true, completion: nil) }
If you want to validate the input box, add delegate to the UITextField and you can validate as normally we do for others.
iBlahji/Example-UIAlertController
Example-UIAlertController - UIAlertController in swiftgithub.com
"Good software, like wine, takes time" - Joel Spolsky 🍷