IRAlertManager 1.0.1

IRAlertManager 1.0.1

irons163 维护。



  • 作者
  • irons163

Build Status Platform

IRAlertManager

  • IRAlertManager 是 iOS 上一个强大的警报管理器,可以处理不同的警报框架。

特点

  • 处理不同的警报框架。
  • 支持全局加载视图。

安装

Git

  • 使用 Git 克隆此项目。
  • 将此项目复制到您的项目中。
  • 将 .xcodeproj 文件添加到您项目中,并将其作为嵌入式框架链接。

选项

  • 您可以删除 demoScreenShots 文件夹。

Cocoapods

  • Podfile中添加pod 'IRAlertManager'
  • 运行pod install

使用方法

基本

  • demo展示了三种不同的提醒系统,包括Apple系统提醒XFDialogBuilderLGAlertView

  • 假设你想处理名为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;
  • IRAlertActionDEMOAlert工作,这是最困难的部分,你需要了解如何使用DEMOAlert,并让DEMOAlert调用IRAlertAction的处理程序。在这个例子中,DEMOAlertactionHandlercancelHandlerdestructiveHandler,每个处理程序都与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的自定义视图
Show Alert for XFDialogBuilder Show Custom View for XFDialogBuilder
显示LGAlert的提醒 显示LGAlert的自定义视图
Show Alert for LGAlert Show Custom View for LGAlert
显示系统操作表 显示系统提示框
Show System Action Sheet Show System Alert
主页面 显示带加载页面的系统提示框
Main Page Show System Alert And Loding Page