Bugle
Bugle,是一个显示警报的助手,无需编写样板代码。
安装
CocoaPods
CocoaPods是一个用于 Swift 和 Objective-C Cocoa 项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
要使用示例项目尝试 Bugle
,请运行以下命令
$ pod try Bugle
要将 Bugle
集成到您的 Xcode 项目中,在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
use_frameworks!
target 'MyApp' do
pod 'Bugle', '~> 2.0'
end
然后,运行以下命令
$ pod install
简单用法
在你的视图控制器中使用
let alert = UIAlertController(style: .alert, title: "Bugle", message: "This is an alert with title")
alert.addAction(title: "OK", style: .cancel)
alert.show()
自定义
由于这个版本直接依赖于 UIAlertController
扩展,您可以创建一个包含您自己的扩展方法的文件
extension String {
// NOTE: Maybe you could use localization approaches here.
static let defaultAlertTitle = "Bugle"
static let defaultAlertActionLabel = "Understood"
}
// MARK: - Initializers
extension UIAlertController {
/// Create new alert view controller.
///
/// - Parameters:
/// - message: alert controller's message (default is nil).
/// - tintColor: alert controller's tint color (default is nil)
convenience init(message: String? = nil, tintColor: UIColor? = nil) {
self.init(
title: String.defaultAlertTitle,
message: message,
preferredStyle: .alert
)
}
}
extension UIAlertController {
/// Displays an alert view by adding the default action in order to be able to dismiss it.
func play() {
self.addAction(UIAlertAction(title: String.defaultAlertActionLabel, style: .default, handler: nil))
self.show()
}
}
然后
let alert = UIAlertController(message: "Hello World!")
alert.play()
在此处查看完整版本:完整版本
贡献者
许可
Bugle 采用 MIT 许可发布。有关详细信息,请参阅 LICENSE。