IRAlertManager
- IRAlertManager 是 iOS 上一个强大的警报管理器,可以处理不同的警报框架。
特点
- 处理不同的警报框架。
- 支持全局加载视图。
安装
Git
- 使用 Git 克隆此项目。
- 将此项目复制到您的项目中。
- 将 .xcodeproj 文件添加到您项目中,并将其作为嵌入式框架链接。
选项
- 您可以删除
demo
和ScreenShots
文件夹。
Cocoapods
- 在
Podfile
中添加pod 'IRAlertManager'
运行
pod install
使用方法
基本
-
demo
展示了三种不同的提醒系统,包括Apple系统提醒
、XFDialogBuilder
和LGAlertView
。 -
假设你想处理名为
DEMOAlert
的提醒系统,你可以按照以下步骤操作 -
创建一个新的类
IRDEMOAlert
,它继承自IRAlert
并导入IRAlertManager
#import <IRAlertManager/IRAlertManager.h>
typedef NS_ENUM(NSUInteger, IRDEMOAlertStyle) {
IRDEMOAlertStyleAlert = 0,
IRDEMOAlertStyleActionSheet = 1
};
@interface IRDEMOAlert : IRAlert
- (instancetype)initWithTitle:(nullable NSString *)title
message:(nullable NSString *)message
style:(IRDEMOAlertStyle)style
buttonActions:(nullable NSArray<IRAlertAction *> *)buttonActions
cancelButtonAction:(nullable IRAlertAction *)cancelButtonAction
destructiveButtonAction:(nullable IRAlertAction *)destructiveButtonAction;
@end
- 在内部编写创建
DEMOAlert
实例的初始代码
- (instancetype)initWithTitle:(nullable NSString *)title
message:(nullable NSString *)message
style:(IRAlertLGStyle)style
buttonActions:(nullable NSArray<IRAlertAction *> *)buttonActions
cancelButtonAction:(nullable IRAlertAction *)cancelButtonAction
destructiveButtonAction:(nullable IRAlertAction *)destructiveButtonAction {
if(self = [super init]){
NSMutableArray<NSString *> * titles = [self titlesWithButtonActions:buttonActions];
alert = [[DEMOAlert alloc] initWithTitle:title message:message style:(DEMOAlertStyle)style buttonTitles:titles cancelButtonTitle:cancelButtonAction.title destructiveButtonTitle:destructiveButtonAction.title];
[self setupButtonActions:buttonActions cancelButtonAction:cancelButtonAction destructiveButtonAction:destructiveButtonAction];
[self registerForKeyboardNotifications];
}
return self;
}
- 重写
IRAlert
方法
-(void)setBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor;
-(void)setCornerRadius:(CGFloat)cornerRadius;
-(void)addAction:(IRAlertAction*)action;
- 让
IRAlertAction
为DEMOAlert
工作,这是最困难的部分,你需要了解如何使用DEMOAlert
,并让DEMOAlert
调用IRAlertAction
的处理程序。在这个例子中,DEMOAlert
有actionHandler
、cancelHandler
和destructiveHandler
,每个处理程序都与IRAlertAction
映射
- (void)setupButtonActions:(NSArray<IRAlertAction *> * _Nullable)buttonActions cancelButtonAction:(IRAlertAction * _Nullable)cancelButtonAction destructiveButtonAction:(IRAlertAction * _Nullable)destructiveButtonAction {
__weak IRDEMOAlert* wself = self;
alert.actionHandler = ^(DEMOAlert * _Nonnull alertView, NSUInteger index, NSString * _Nullable title) {
if(index >= buttonActions.count)
return;
IRAlertAction *action = [buttonActions objectAtIndex:index];
action.handler(action);
};
alert.cancelHandler = ^(DEMOAlert * _Nonnull alertView) {
if (cancelButtonAction) {
cancelButtonAction.handler(cancelButtonAction);
}
};
alert.destructiveHandler = ^(DEMOAlert * _Nonnull alertView) {
if (destructiveButtonAction) {
destructiveButtonAction.handler(destructiveButtonAction);
}
};
}
- 使用
IRAlertManager
显示或隐藏提示框。
[[IRAlertManager sharedInstance] showAlert:alert];
[[IRAlertManager sharedInstance] hideAlert:alert];
- 使用
IRAlertManager
显示或隐藏全局加载视图。
[[IRAlertManager sharedInstance] showLoadingViewWithTarget:self backgroundImage:[ViewController imageWithColor:[UIColor greenColor] Size:[UIScreen mainScreen].bounds.size]];
[[IRAlertManager sharedInstance] hideLoadingViewWithTarget:self];
- 使用加载视图显示
DEMOAlert
。
- (IBAction)showDEMOAlert:(id)sender {
IRAlertAction *commitAction = [[IRAlertAction alloc] init];
commitAction.title = @"OK";
commitAction.style = IRAlertActionStyleDefault;
IRAlertAction *cancelAction = [[IRAlertAction alloc] init];
cancelAction.title = @"Cancel";
cancelAction.style = IRAlertActionStyleCancel;
alert = [[IRDEMOAlert alloc] initWithTitle:nil message:nil style:IRDEMOAlertStyleAlert buttonActions:@[commitAction] cancelButtonAction:cancelAction destructiveButtonAction:nil];
__weak IRAlert *wAlert = alert;
commitAction.handler = ^(IRAlertAction * _Nonnull action) {
[[IRAlertManager sharedInstance] hideAlert:wAlert];
};
__weak ViewController *wSelf = self;
cancelAction.handler = ^(IRAlertAction * _Nonnull action) {
[[IRAlertManager sharedInstance] hideLoadingViewWithTarget:wSelf];
};
[alert setCornerRadius:20];
[[IRAlertManager sharedInstance] showAlert:alert];
}
屏幕截图
显示XFDialogBuilder的提醒 | 显示XFDialogBuilder的自定义视图 |
---|---|
![]() |
![]() |
显示LGAlert的提醒 | 显示LGAlert的自定义视图 |
![]() |
![]() |
显示系统操作表 | 显示系统提示框 |
![]() |
![]() |
主页面 | 显示带加载页面的系统提示框 |
![]() |
![]() |