LKAlertController 1.12.2

LKAlertController 1.12.2

测试已测试
Lang语言 SwiftSwift
许可证 MIT
Released最新释放2018年11月
SPM支持SPM

Erik SargentKevin MannShane Thompson维护。



  • 作者
  • Erik Sargent

LKAlertController

Circle CI Version License Platform

Swift的可使用UIAlertController构建器

功能

  • 提供创建Adjusted和ActionSheets从UIAlertController的简短简单语法
  • 通过组合方法构建更复杂的alert和action sheets

基本用法

Alert

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

详细用法

有两组分离类用于创建 UIAlertControllersAlertActionSheet。这些用于简化控制器的创建。两者均可以带或不带标题和消息进行初始化。

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

使用 addAction 为控制器添加各种操作。默认情况下,按钮将被样式化为 取消,但可以按每个按钮进行配置,包括当按钮被点击时调用的处理程序。可能的样式有 取消默认破坏性。可以将这些方法串联起来为控制器添加更多按钮。

ActionSheet()
	.addAction("Cancel")
	.addAction("Save", style: .Default) {
		saveTheObject()
	}
	.addAction("Delete", style: Destructive) {
		deleteTheObject()
	}

通过调用 show() 函数来显示控制器。它将默认进行动画。

Alert()
	.addAction("Okay")
	.show()
	
ActionSheet()
	.addAction("Delete", style: .Destructive) {
		delete()
	}
	.addAction("Cancel")
	.show(animated: true) {
		controllerWasPresented()
	}

Alert 专用配置

在 alerts 中有一个快捷方式显示带有一个 OK 按钮:showOkay

Alert(title: "Stuff has happened").showOkay()

您还可以添加自己的快捷方式显示方法。以下添加了一个 showNevermind 按钮,该按钮添加一个 Nevermind 按钮,并显示该 alert。

extension Alert {
	///Shortcut method for adding a nevermind button and showing the alert
	public func showNevermind() {
		addAction("Nevermind", style: .Cancel, preferredAction: false, handler: nil)
		show()
	}
}

文本字段也可以添加到 alerts 中。要添加文本字段,首先初始化一个文本字段,然后对其进行配置,然后将其与 alert 一起传递。请注意,文本字段必须作为 var 而不是 let 初始化。

var textField = UITextField()
textField.placeholder = "Password"
textField.secureTextEntry = true

Alert().addTextfield(&textField).showOkay()

您还可以配置 alert 的 preferredAction 属性。这将突出显示操作的文本,并且在物理键盘上的回车键按下的情况下将触发此操作。

Alert()
	.addAction("Okay", style: .Default, preferredAction: true)
	.show()

操作表专用配置

如果要在 iPad 上显示,需要配置 ActionSheets 的显示位置。这是通过使用 setBarButtonItemsetPresentingSource 函数来完成的。请注意,这对 iPhone 没有影响,因此如果您的应用程序同时支持 iPad 和 iPhone,调用此方法是安全且推荐的。

ActionSheet()
	.addAction("Delete", style: .Destructive) {
		delete()
	}
	.addAction("Cancel")
	.setBarButtonItem(navigationController.rightBarButtonItem)
	.show(animated: true) {
		controllerWasPresented()
	}
	
ActionSheet()
	.addAction("Delete", style: .Destructive) {
		delete()
	}
	.addAction("Cancel")
	.setPresentingSource(buttonThatWasPressed)
	.show(animated: true) {
		controllerWasPresented()
	}

## 测试

您可以为 show 方法添加一个覆盖功能,以便轻松为您的警报添加单元测试。

func testDeleteOpensConfirmationAlert() {
	let expectation = expectationWithDescription("Show override")

	LKAlertController.overrideShowForTesting { (style, title, message, actions, fields) -> 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 通过 CocoaPods 提供。要安装它,只需在您的 Podfile 中添加以下行

pod "LKAlertController"

Carthage

Carthage 是一款去中心化的依赖项管理器,它构建您的依赖关系并提供二进制框架。

您可以使用以下命令使用 Homebrew 安装 Carthage

$ brew update
$ brew install carthage

要使用 Carthage 在您的 Xcode 项目中集成 LKAlertController,请在您的 Cartfile 中指定它

github "lightningkite/LKAlertController"

运行 carthage update 以构建框架,并将构建的 LKAlertController.framework 拖拽到您的 Xcode 项目中。

问题、疑问和贡献

有问题,或想要求一个功能吗?在 GitHub 上创建一个 issue。

想要贡献吗?加入作者名单,并创建一个 pull request。

作者

Erik Sargent,[email protected]

许可协议

LKAlertController 根据 MIT 许可协议提供。有关更多信息,请参阅 LICENSE 文件。