ASJAlertController
UIAlertController
相比于旧的 UIAlertView
有很大的改进。但是,它没有简单的 show
方法来使它显示在屏幕上,必须在一个视图控制器上显示。像我一样,如果您怀念旧的方式展示提示框,这个小库将很有帮助。
这是一个添加了 show
方法以及创建提示框和操作表单的便捷方法的 UIAlertController
类别。
它通过在应用的主窗口之上创建一个新的 UIWindow
并且在其上显示一个 UIAlertController
来工作。这是苹果公司据说在自家实现中使用的方法。
安装
CocoaPods是目前安装此库的首选方式。将此命令添加到您的 Podfile
中
pod 'ASJAlertController'
使用方法
显示提示框
[ASJAlertController showAlertWithTitle:@"Title"
message:@"Message"
cancelButtonTitle:@"Cancel"
tapHandler:^(ASJAlertAction * _Nullable action, NSString * _Nullable buttonTitle)
{
// handle button taps here
}];
展示操作表
[ASJAlertController showActionSheetWithTitle:@"Title"
message:@"Message"
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Destroy"
otherTitles:@[@"Button 1", @"Button 2"]
tapHandler:^(ASJAlertAction * _Nullable action, NSString * _Nullable buttonTitle)
{
// handle button taps here
}];
ASJAlertButton
显示 alert/action sheet
使用 ASJAlertButton *button1 = [ASJAlertButton buttonWithTitle:@"Button 1" style:ASJAlertActionStyleDestructive];
ASJAlertButton *button2 = [ASJAlertButton buttonWithTitle:@"Button 2" style:ASJAlertActionStyleDefault];
[ASJAlertController showAlertWithTitle:@"Test"
message:@"A test action sheet"
cancelButtonTitle:@"Cancel"
otherButtons:@[button1, button2]
preferredStyle:ASJAlertControllerStyleActionSheet
tapHandler:^(ASJAlertAction * _Nullable action, NSString * _Nullable buttonTitle)
{
// handle button taps here
}];
手动显示
您还可以选择不立即显示 alert。使用 show
方法实现此功能。对于 otherButtons
,您需要根据使用的方法传递一个 ASJAlertButton
类型的 NSArray
或 NSString
。您可以将不需要的任何参数保持为 nil
。
ASJAlertController *alert = [ASJAlertController alertWithTitle:@"Title"
message:@"Message"
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherTitles:nil
preferredStyle:ASJAlertControllerStyleAlert
tapHandler:^(ASJAlertAction * _Nullable action, NSString * _Nullable buttonTitle)
{
// handle button taps here...
}];
[alert show];
致谢
许可
ASJAlertController
在 MIT 许可证下可用。更多信息请参阅 LICENSE 文件。