Skip to content

akwilliamson/LKAlertController

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LKAlertController

Circle CI Version License Platform

An easy to use UIAlertController builder for swift

Features

  • Short and simple syntax for creating both Alerts and ActionSheets from UIAlertController
  • String together methods to build more complex alerts and action sheets

Basic Usage

Alert


Alert(title: "Title", message: "Message")
	.addAction("Cancel")
	.addAction("Delete", style: .Destructive, handler: { _ in
		//Delete the object
	}).show()

Action Sheet


ActionSheet(title: "Title", message: "Message")
	.addAction("Cancel")
	.addAction("Delete", style: .Destructive, handler: { _ in
		//Delete the object
	}).show()

Detailed Usage

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)
}

Installation

LKAlertController is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "LKAlertController"

Issues Questions and Contributing

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.

Author

Erik Sargent, erik@lightningkite.com

License

LKAlertController is available under the MIT license. See the LICENSE file for more info.

About

An easy to use UIAlertController builder for swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 75.1%
  • Shell 18.5%
  • Ruby 3.3%
  • Objective-C 1.9%
  • C 1.2%