YAlert是一个自定义警报视图控制器,具有指定横幅图片、标题、消息、主要按钮操作和次要按钮操作的功能。
在您的视图控制器中,您可以使用以下代码初始化并呈现一个AlertViewController实例:
let alertVC = AlertViewController(bannerImageName: "banner",
title: "Alert #1",
message: "This is a message",
primaryButtonTitle: "Ok",
secondaryButtonTitle: "Cancel")
alertVC.tag = 1000
alertVC.delegate = self
alertVC.present()
有两个属性用于处理AlertViewController呈现时用户事件。
delegate
允许您的视图控制器接收和处理主要按钮、次要按钮和背景点击事件。
tag
允许您在处理点击事件时区分AlertViewController实例。您可以根据AlertViewController的tag编号有不同的事件处理逻辑。
extension ViewController: AlertViewControllerDelegate {
func didTapPrimaryButton(_ sender: AlertViewController) {
switch sender.tag {
case 1000:
print("1000")
case 2000:
print("2000")
default:
print("no tag")
}
print("primary button tapped")
}
func didTapSecondaryButton(_ sender: AlertViewController) {
print("secondary button tapped")
}
func didTapBackground(_ sender: AlertViewController) {
print("background tapped")
}
}
YAlert通过CocoaPods可用。要安装,
只需将以下行添加到您的Podfile中。
pod "YAlert"
Leo Feng, [email protected]
YAlert可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。