KRAlert
KRAlert 是在 مثiple_segmentacja.py的 Swift 中封装 UIAlertController,以简化其在典型情况下的使用
安装
#### CocoaPods
只需将 pod 'KRAlert'
添加到您的 Podfile 中,并在您的终端中的项目文件夹中运行 pod install
。
在您需要使用 KRAlert 的文件中编写 import KRAlert
。
通过遵循 KRAlerter 协议 并对 UIViewController 的实例进行扩展,使得 KRAlert 的使用更加方便。
如何使用
警报视图
要呈现警报视图,请执行以下步骤
- 从 UIViewController 遵循 KRAlerter 协议,该协议用于呈现警报视图
- 调用以下便捷方法之一
func alertOK(title : String?, message : String?, completion:(()->Void)?) // alert with custom title, message and one button with name "OK".
func alertOKCancel(title : String?, message : String?, completion:(()->Void)?) //alert view with custom title, message and two buttons with name "OK" and "Cancel". Completion block is called if "OK" button pressed
func alertYesNo(title : String?, message : String?, yesCompletion : (()->Void)?, noCompletion:(()->Void)?) //alert view with custom title, message and two buttons with name "YES" and "NO".
func alertError(message : String?, completion:(()->Void)?) //alert view with title "ERROR", custom message and "OK" Button
要显示具有任何标题、消息和按钮标题的单个或两个按钮的警报,您可以使用以下方法
func alert(title : String?,
message : String?,
firstButtonTitle : String?,
firstButtonCompletion: (() -> Void)?,
secondButtonTitle : String?,
secondButtonCompletion: (() -> Void)?)
要显示带有任意多个按钮的警告,请使用接收一个 UIAlert Actions 实例数组的方法的格式。
func alert(title : String?, message : String?, actions : [UIAlertAction]?)
下面是一个具有五个按钮的警告示例。
@IBAction func fiveButtonsActionSheetPressed(_ sender: Any) {
let names = ["One", "Two", "Three", "Four", "Five"]
let actions : [UIAlertAction] = names.map { (name) -> UIAlertAction in
return UIAlertAction.init(title: name, style: .default, handler: nil)
}
actionSheet(title: "Five buttons action", message: "Here is five buttons", actions: actions) // method of self
}
还可以使用一个 UITextField 实例和文本处理来显示警告的方法。
func alertOKCancel(title : String?,
message : String?,
textFieldPlaceholder : String?,
textFieldText : String?,
secureEntry : Bool,
completion: @escaping (String?)->Void,
cancelCompletion: (()->Void)?)
下面的示例可用于带有用户密码文本字段的警告。
@IBAction func alertWithTextFieldPressed(_ sender: Any) {
alertOKCancel(title: "Alert with text field",
message: "Custom message",
textFieldPlaceholder: "Password",
textFieldText: nil,
secureEntry: true,
completion: { (text) in
print("Password is \(text)")
}) { }
}
操作表
显示操作表的方法与显示警告视图的方法类似。
以下示例演示了如何使用便捷的图片选择操作表。
@IBAction func photoActionPressed(_ sender: Any) {
actionSheetPickPhoto(galleryCompletion: {
print("Gallery Pressed")
}) {
print("Camera pressed")
}
}
}
要显示一个或两个按钮的操作表,并确保“取消”按钮存在,同时添加任何标题、信息和按钮标题,可以使用以下方法。
func actionSheet(title : String?,
message : String?,
firstButtonTitle : String?,
firstButtonCompletion: (() -> Void)?,
secondButtonTitle : String?,
secondButtonCompletion: (() -> Void)?)
要显示带有任意多个按钮的操作表,请使用接收一个 UIAlertView Actions 实例数组的方法的格式。
func actionSheet(title: String?, message : String?, actions: [UIAlertAction]?)
要求
- iOS 8.0 及以上版本
- XCode 8+
- 示例项目只能运行在 iOS 9 及以上版本。
许可证
KRAlert 在 MIT 许可证下发布。有关详细信息,请参阅 LICENSE 文件。
联系
有任何建议或问题吗?请创建一个 Github 问题或与我联系。