An easy to use UIAlertController builder for swift
- Short and simple syntax for creating both Alerts and ActionSheets from UIAlertController
- String together methods to build more complex alerts and action sheets
Alert(title: "Title", message: "Message")
.addAction("Cancel")
.addAction("Delete", style: .Destructive, handler: { _ in
//Delete the object
}).show()ActionSheet(title: "Title", message: "Message")
.addAction("Cancel")
.addAction("Delete", style: .Destructive, handler: { _ in
//Delete the object
}).show()There are two seperate classes for creating UIAlertControllers, Alert, and ActionSheet. These are used to simplify the creation of the controller. Both can be initialized with or without both a title and message.
Alert()
ActionSheet()
Alert(title: "My title")
ActionSheet(title: "My title")
Alert(message: "My message")
ActionSheet(message: "My message")
Alert(title: "My title", message: "My message")
ActionSheet(title: "My title", message: "My message")Add various actions to the controller using addAction. By default the button will be styled Cancel, but this can be configured on a per button basis, along with the handler that is called if the button is clicked. The possible styles are Cancel, Default, and Destructive. These methods can be strung together to add more buttons to the controller.
ActionSheet()
.addAction("Cancel")
.addAction("Save", style: .Default) {
saveTheObject()
}
.addAction("Delete", style: Destructive) {
deleteTheObject()
}The controller can be presented by calling show(). It will be animated by default.
Alert()
.addAction("Okay")
.show()
ActionSheet()
.addAction("Delete", style: .Destructive) {
delete()
}
.addAction("Cancel")
.show(animated: true) {
controllerWasPresented()
}There is also a shortcut for quickly showing an alert with an Okay button. After initializing an alert, call showOkay
Alert(title: "Stuff has happened").showOkay()##Testing
You can add an override for the show method to make it easy to add unit tests for your alerts.
func testDeleteOpensConfirmationAlert() {
let expectation = expectationWithDescription("Show override")
LKAlertController.overrideShowForTesting { (style, title, message, actions) -> Void in
XCTAssertEquals(title, "Are you sure you want to delete?", "Alert title was incorrect")
expectation.fulfill()
}
model.delete()
//If the override is never called, and the expectation is not fulfilled, the test will fail
waitForExpectations(0.5, handler: nil)
}LKAlertController is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "LKAlertController"Have an issue, or want to request a feature? Create an issue in github.
Want to contribute? Add yourself to the authors list, and create a pull request.
Erik Sargent, erik@lightningkite.com
LKAlertController is available under the MIT license. See the LICENSE file for more info.