GTAlertCollection
open class GTAlertCollection : NSObject
A collection of several UIAlertController
variations gathered in one place.
With GTAlertCollection
you can present fast, easily and efficiently:
- One-button alert controller
- Alert controller with variable number of buttons with all supported styles: Default, Cancel, Destructive
- Buttonless alert controller
- Alert controller with an activity indicator
- Alert controller with a progress bar, including text representation of the progress (either percentage, or number of steps made)
- Alert controller with a single text field
- Alert controller with multiple text fields
- Alert controller with an image view
See more on GitHub
-
The
GTAlertCollection
shared instance.Declaration
Swift
public static let shared: GTAlertCollection
-
The presented alert controller.
Declaration
Swift
public var alertController: UIAlertController!
-
The view controller that the alert controller is presented to.
Declaration
Swift
public var hostViewController: UIViewController!
-
The activity indicator of the alert.
Declaration
Swift
public var activityIndicator: UIActivityIndicatorView!
-
The
UIProgressView
object that displays the progress bar.Declaration
Swift
public var progressBar: UIProgressView!
-
The label right below the progress bar.
Declaration
Swift
public var label: UILabel!
-
The image view of the alert.
Declaration
Swift
public var imageView: UIImageView!
-
Custom
init
method that accepts the host view controller as an argument.You are advised to use this initializer when creating objects of this class and you are not using the shared instance.
Declaration
Swift
public init(withHostViewController hostViewController: UIViewController)
Parameters
hostViewController
The view controller that will present the alert controller.
-
It dismisses the alert controller.
Declaration
Swift
public func dismissAlert(completion: (() -> Void)?)
Parameters
completion
If the
completion
is notnil
, it’s called to notify that the alert has been dismissed. -
It presents the alert controller with one action button only.
Declaration
Swift
public func presentSingleButtonAlert(withTitle title: String?, message: String?, buttonTitle: String, actionHandler: @escaping () -> Void)
Parameters
title
The optional title of the alert.
message
The optional message of the alert.
buttonTitle
The action button title. Required.
actionHandler
It’s called when the action button is tapped by the user. Use it to implement the required logic after the user has tapped on your alert’s button.
-
presentAlert(withTitle:message:buttonTitles:cancelButtonIndex:destructiveButtonIndices:actionHandler:)
It presents an alert controller with as many action buttons as the
buttonTitles
array items, using the provided title and message for the displayed text. It can have one Cancel-styled and one or more Destructive-styled action buttons.Declaration
Swift
public func presentAlert(withTitle title: String?, message: String?, buttonTitles: [String], cancelButtonIndex: Int?, destructiveButtonIndices: [Int]?, actionHandler: @escaping (_ actionIndex: Int) -> Void)
Parameters
title
The optional title of the alert.
message
The optional message of the alert.
buttonTitles
The array with the action button titles. Give the titles in the order you want them to appear.
cancelButtonIndex
If there’s a Cancel-styled button you want to exist among buttons, then here’s the place where you specify it’s position in the
buttonTitles
array. Passnil
if there’s no Cancel-styled button.destructiveButtonIndices
An array with the position of one or more destructive buttons in the collection of buttons. Pass
nil
if you have no destructive buttons to show.actionHandler
Use this block to determine which action button was tapped by the user. An
actionIndex
value provides you with that information. -
It presents a buttonless alert controller.
Declaration
Swift
public func presentButtonlessAlert(withTitle title: String?, message: String?, presentationCompletion: @escaping (_ success: Bool) -> Void)
Parameters
title
The optional title of the alert.
message
The optional message of the alert.
presentationCompletion
Called after the alert controller has been presented or when the
hostViewController
isnil
, and indicates whether the alert controller was presented successfully or not.success
When
true
the alert controller has been successfully presented to the host view controller, otherwise it’sfalse
. -
presentActivityAlert(withTitle:message:activityIndicatorColor:showLargeIndicator:presentationCompletion:)
It presents a buttonless alert controller with an activity indicator in it. The indicator’s color and size can be optionally customized.
Implementation Example:
GTAlertCollection.shared.presentActivityAlert(withTitle: "Please wait", message: "You are being connected to your account...", activityIndicatorColor: .blue, showLargeIndicator: false) { (success) in if success { // The alert controller was presented successfully... } }
Declaration
Swift
public func presentActivityAlert(withTitle title: String?, message: String?, activityIndicatorColor: UIColor = UIColor.black, showLargeIndicator: Bool = false, presentationCompletion: @escaping (_ success: Bool) -> Void)
Parameters
title
The optional title of the alert.
message
The optional message of the alert.
activityIndicatorColor
The color of the activity indicator. By default, the color is set to black.
showLargeIndicator
Pass
true
when you want to use the large indicator style,false
when you want to display the small one. Default value isfalse
.presentationCompletion
Called after the alert controller has been presented or when the
hostViewController
isnil
, and indicates whether the alert controller was presented successfully or not.success
When
true
the alert controller has been successfully presented to the host view controller, otherwise it’sfalse
. -
presentSingleTextFieldAlert(withTitle:message:doneButtonTitle:cancelButtonTitle:configurationHandler:completionHandler:)
It presents an alert controller that contains a text field, and two action buttons (Done & Cancel). Title and message are included too. The action button titles are modifiable.
Implementation Example:
GTAlertCollection.shared.presentSingleTextFieldAlert(withTitle: "Editor", message: "Type something:", doneButtonTitle: "OK", cancelButtonTitle: "Cancel", configurationHandler: { (textField, didFinishConfiguration) in if let textField = textField { // Configure the textfield properties. textField.delegate = self textField.placeholder = "Write here" // ... more configuration ... // Always call the following to let the alert controller appear. didFinishConfiguration() } }) { (textField) in if let textField = textField { // Do something with the textfield's text. } }
Declaration
Swift
public func presentSingleTextFieldAlert(withTitle title: String?, message: String?, doneButtonTitle: String = "Done", cancelButtonTitle: String = "Cancel", configurationHandler: @escaping (_ textField: UITextField?, _ didFinishConfiguration: () -> Void) -> Void, completionHandler: @escaping (_ editedTextField: UITextField?) -> Void)
Parameters
title
The optional title of the alert.
message
The optional message of the alert.
doneButtonTitle
The Done button title. Default value is
Done
.cancelButtonTitle
The Cancel button title. Default value is
Cancel
.configurationHandler
A closure where you can configure the text field’s properties before the alert is presented. The first parameter of the closure is the textfield itself, the second parameter it’s a closure that must be called manually when you finish configuring the textfield, so it’s possible for the alert to be presented.
completionHandler
Called when the Done button is tapped. It contains the textfield as a parameter.
didFinishConfiguration
Calling this closure from your code is mandatory. You do that after having configured the text field. If you don’t make any configuration, call it directly in the
configurationHandler
closure.textField
The single text field of the alert. Be careful with it and always unwrap it before using it, as it can be
nil
.editedTextField
The single text field of the alert as returned when the Done button gets tapped. Always unwrap it before using it as it might be
nil
. -
presentMultipleTextFieldsAlert(withTitle:message:doneButtonTitle:cancelButtonTitle:numberOfTextFields:configurationHandler:completionHandler:)
It presents an alert controller with variable number of text fields, as well as a Done and a Cancel action button. Titles of both are modifiable. Alert title and message included too.
Implementation Example:
GTAlertCollection.shared.presentMultipleTextFieldsAlert(withTitle: "Input Data", message: "Lots of stuff to type here...", doneButtonTitle: "OK", cancelButtonTitle: "Cancel", numberOfTextFields: 5, configurationHandler: { (textFields, didFinishConfiguration) in if let textFields = textFields { let placeholders = ["First name", "Last name", "Nickname", "Email", "Password"] for i in 0..<textFields.count { textFields[i].placeholder = placeholders[i] } textFields.last?.isSecureTextEntry = true // ... more configuration ... didFinishConfiguration() } }) { (textFields) in if let textFields = textFields { // Do something with the textfields. } }
Declaration
Swift
public func presentMultipleTextFieldsAlert(withTitle title: String?, message: String?, doneButtonTitle: String = "Done", cancelButtonTitle: String = "Cancel", numberOfTextFields: Int, configurationHandler: @escaping (_ textFields: [UITextField]?, _ didFinishConfiguration: () -> Void) -> Void, completionHandler: @escaping (_ editedTextFields: [UITextField]?) -> Void)
Parameters
title
The optional title of the alert.
message
The optional message of the alert.
doneButtonTitle
The Done button title. Default value is
Done
.cancelButtonTitle
The Cancel button title. Default value is
Cancel
.numberOfTextFields
The number of textfields you want to display on the alert.
configurationHandler
A closure where you can configure the properties of all textfields before the alert is presented. The first parameter of the closure is the collection of the textfields in the alert, the second parameter it’s a closure that must be called manually when you finish configuring the textfields, so it’s possible for the alert to be presented.
completionHandler
Called when the Done button is tapped. It contains the collection of textfields as a parameter.
didFinishConfiguration
Calling this closure from your code is mandatory. You do that after having configured the textfields. If you don’t make any configuration, call it directly in the
configurationHandler
closure.textFields
The collection of the text fields in the alert. Be careful with it and always unwrap it before using it, as it can be
nil
.editedTextFields
The collection of the text fields in the alert as they’re at the moment the Done button gets tapped. Always unwrap it before using it as it might be
nil
. -
presentProgressBarAlert(withTitle:message:progressTintColor:trackTintColor:showPercentage:showStepsCount:updateHandler:presentationCompletion:)
It presents a buttonless alert controller with a progress bar, and optionally a label indicating the progress percentage or the progress steps made out of all steps at any given moment.
The default configuration of the progress view and the label taking place in this method is good enough for most cases. However, if you want to set or modify additional properties on any of those two controls, you can use the class properties
progressBar
andlabel
respectively.Implementation Example:
// Step 1: In your class declare the following property: var updateHandler: ((_ currentItem: Int, _ totalItems: Int) -> Void)! // Step 2: Implement the alert presentation somewhere in a method: GTAlertCollection.shared.presentProgressBarAlert(withTitle: "Upload", message: "Your files are being uploaded...", progressTintColor: .red, trackTintColor: .lightGray, showPercentage: true, showStepsCount: false, updateHandler: { (updateHandler) in self.updateHandler = updateHandler }) { (success) in if success { // doRealWork() is a method that makes the actual work and updates the progress as well. self.doRealWork() } } // Step 3: In the doRealWork() method: func doRealWork() { // Implement the method's logic. // When the time comes to update the progress: if let updateHandler = self.updateHandler { // currentStep and totalSteps represent the progress steps. updateHandler(currentStep, totalSteps) } }
Declaration
Swift
public func presentProgressBarAlert(withTitle title: String?, message: String?, progressTintColor: UIColor, trackTintColor: UIColor, showPercentage: Bool, showStepsCount: Bool, updateHandler: @escaping (_ updateHandler: @escaping (_ currentStep: Int, _ totalSteps: Int) -> Void) -> Void, presentationCompletion: @escaping (_ success: Bool) -> Void)
Parameters
title
The optional title of the alert.
message
The optional message of the alert.
progressTintColor
The progress tint color.
trackTintColor
The track tint color.
showPercentage
Make it
true
when you want the progress to be displayed as a percentage right below the progress bar. Make itfalse
to show the items count, or disable the label appearance.showStepsCount
Make it
true
to show the steps that have been made at any given moment, orfalse
when you want to either show the progress as a percentage, or totally disable the label.updateHandler
Use that closure to update the progress.
currentStep
The current step represented by the progress bar among all steps.
totalSteps
The total number of steps until the progress reaches at 100%.
presentationCompletion
Called after the alert controller has been presented or when the
hostViewController
isnil
, and indicates whether the alert controller was presented successfully or not.success
When
true
the alert controller has been successfully presented to the host view controller, otherwise it’sfalse
. -
presentImageViewAlert(withTitle:message:buttonTitles:cancelButtonIndex:destructiveButtonIndices:image:actionHandler:)
It presents an alert controller that contains an image view. It also displays action buttons, and the default title and message of the alert.
To access the image view and set or modify additional properties, use the
imageView
class property.Implementation example:
// Specify your image: // let image = ... // Present the alert. GTAlertCollection.shared.presentImageViewAlert(withTitle: "Your Avatar", message: "What do you want to do with it?", buttonTitles: ["Change it", "Remove it", "Cancel"], cancelButtonIndex: 2, destructiveButtonIndices: [1], image: image) { (actionIndex) in // Handle the actionIndex value... }
Declaration
Swift
public func presentImageViewAlert(withTitle title: String?, message: String?, buttonTitles: [String], cancelButtonIndex: Int?, destructiveButtonIndices: [Int]?, image: UIImage, actionHandler: @escaping (_ actionIndex: Int) -> Void)
Parameters
title
The optional title of the alert.
message
The optional message of the alert.
buttonTitles
The array with the action button titles. Give the titles in the order you want them to appear.
cancelButtonIndex
If there’s a Cancel-styled button you want to exist among buttons, then here’s the place where you specify it’s position in the
buttonTitles
array. Passnil
if there’s no Cancel-styled button.destructiveButtonIndices
An array with the position of one or more destructive buttons in the collection of buttons. Pass
nil
if you have no destructive buttons to show.image
The image to be displayed in the image view.
actionHandler
Use this block to determine which action button was tapped by the user. The
actionIndex
parameter provides you with that information.actionIndex
The index of the tapped action.