BKAlertControllerBuilder
使用 Swift-style 语法来组合 SwiftUI 风格的 UIAlertController
,由现在提交的 Function Builder 驱动。
项目链接:https://github.com/builderkit/BKAlertControllerBuilder
功能
功能 | |
---|---|
使用 Swift 5.1 编写的开源库 | |
类似 SwiftUI 的语法,支持 if-else / ForEach | |
支持 .alert 和 .actionSheet |
|
通过 Swift Package Manager 分发 | |
完全测试过的代码 |
如何使用?
传统上,我们像这样组合 UIAlertController
let greeting = false
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
alert.addAction(.init(title: "Cancel", style: .cancel, handler: nil))
if greeting {
alert.addAction(.init(title: "Hello", style: .default, handler: nil))
} else {
alert.addAction(.init(title: "World", style: .default, handler: nil))
}
present(alert, animated: true, completion: nil)
现在,使用 XLAttributedString,我们可以使用类似 SwiftUI 的语法来声明 NSAttributedString
let greeting = false
UIAlertController(title: "title", message: "message", preferredStyle: .alert) {
Action.cancel("Cancel")
if greeting {
Action.default("Hello", handler: nil)
} else {
Action.default("World", handler: nil)
}
}.presented(by: self, animated: true)
所需条件
Xcode 11。该项目使用 Swift 5.1 特性 Function Builder。
安装
Swift 包
在 Xcode 11 中打开您的项目,导航到菜单 -> Swift 包 -> 添加包依赖,并输入https://github.com/builderkit/BKAlertControllerBuilder进行安装。
CocoaPods
在您的 Podfile
中添加 pod 'BKAlertControllerBuilder'
。
演示应用
打开BKAttributedStringBuilder.xcworkspace,并运行BKAttributedStringBuilderDemoApp
目标。