从零开始设计搭建ios App框架(三)

为App添加消息提示框


所有的App都会有消息提示,如简单的提示框、询问框。此文不是介绍如何自定义消息提示框,消息提示使用ios自带的UIAlertView、UIAlertController。
也许有人立即想到,这太简单了,马上写给你看。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:actionTitles, nil];
[alert show];

//回调
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //事件处理
}

相信大家一个App里面消息提示肯定会有好多,难道在每个使用的地方加上如下代码?这样不觉得重复的代码比较多吗,我们常说代码复用,是不是可以重新设计下,提升一下代码复用性呢?当然是可以的。
在我看来,大多数的数据流转,业务逻辑都会扯到ViewController,这样就会使用Controller变胖,大神们都会了解MVC、MVP、MVVM设计模式,我理解不是很透,我也不多说什么了。我认为一个好的App框架的设计是要满足功能模块化,低偶合,代码复用性强,调用方便简洁,可扩展性就很不错了。
哈哈,不扯远了,入正题。只需要在ViewController扩展几个方法。

/*
 消息提示,错误提示
 */
@interface PGBaseController (errorMsgView)

#pragma mark message
- (void)showMsg:(NSString *)szMsg;
- (void)showTitle:(NSString *)szTitle msg:(NSString *)szMsg;
- (void)showAskAlertTitle:(NSString *)title
                  message:(NSString *)message
                      tag:(NSInteger)tag
                   action:(void(^)(NSInteger alertTag, NSInteger actionIndex))block
        cancelActionTitle:(NSString *)cancelTitle
       otherActionsTitles:(NSString *)actionTitles,...;

@end

方法实现

#pragma mark message
- (void)showMsg:(NSString *)szMsg {
    [self showTitle:nil msg:szMsg];
}

- (void)showTitle:(NSString *)szTitle msg:(NSString *)szMsg {
    [self showAskAlertTitle:szTitle message:szMsg tag:0 action:nil cancelActionTitle:@"确定" otherActionsTitles:nil];
}

- (void)showAskAlertTitle:(NSString *)title
                  message:(NSString *)message
                      tag:(NSInteger)tag
                   action:(void(^)(NSInteger alertTag, NSInteger actionIndex))block
        cancelActionTitle:(NSString *)cancelTitle
       otherActionsTitles:(NSString *)actionTitles,... {
    
    NSMutableArray *arrayTitles = [[NSMutableArray alloc] init];
    [arrayTitles addObject:cancelTitle];
    
    NSString *szActionTitle = nil;
    va_list argumentList;
    if(actionTitles) {
        [arrayTitles addObject:actionTitles];
        va_start(argumentList, actionTitles);
        szActionTitle = va_arg(argumentList, NSString *);
        while(szActionTitle) {
            [arrayTitles addObject:szActionTitle];
            szActionTitle = va_arg(argumentList, NSString *);
        }
        
        va_end(argumentList);
    }
    
    if(IOS8_LATER) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        for(NSInteger i = 0; i < arrayTitles.count; i++)
        {
            NSString *string = [arrayTitles objectAtIndex:i];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:string style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if(block)
                {
                    block(tag, i);
                }
            }];
            [alertController addAction:okAction];
        }
        [self presentViewController:alertController animated:YES completion:nil];
    } else {
#if __IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_8_0
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:actionTitles, nil];
        alert.alertActionBlock = block;
        [alert show];
#endif
    }
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_8_0
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.alertActionBlock)
    {
        alertView.alertActionBlock(alertView.tag, buttonIndex);
    }
}
#endif

仔细看代码的同学会发现这句 alertView.alertActionBlock(alertView.tag, buttonIndex); 印象中UIAlertView没有这么个东西!!!这是使用了…… 对,没错就是Catagory+runtime。

UIAlertView+action.h

#import 

typedef void(^PGAlertActionBlock)(NSInteger alertTag, NSInteger actionIndex);

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
@interface UIAlertView (action)

@property(nonatomic, copy)PGAlertActionBlock alertActionBlock;

@end

#endif

UIAlertView+action.m

#import "UIAlertView+action.h"
#import 

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
//定义常量 必须是C语言字符串
static char *actionNameKey = "actionNameKey";
@implementation UIAlertView (action)

- (void)setAlertActionBlock:(PGAlertActionBlock)alertActionBlock
{
    objc_setAssociatedObject(self, actionNameKey, alertActionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (PGAlertActionBlock)alertActionBlock
{
    return objc_getAssociatedObject(self, actionNameKey);
}

@end

#endif

如何使用呢?只需在有需要显示的地方调用下面的方法就可以实现相应的功能。

[self showMsg:@"消息内容"];
[self showAskAlertTitle:@"标题" message:@"提示的内容" tag:0 action:^(NSInteger alertTag, NSInteger actionIndex) {
        //事件响应
        if(actionIndex == 0) {
            
        } else if(actionIndex == 1) {
            
        }
    } cancelActionTitle:@"取消" otherActionsTitles:@"确定",nil];

到现在已经完成此模块的设计,有没有发现,这样使用挺方便的!!呵呵!!写的不好勿喷。

共同学习进步!

上一节:从零开始设计搭建ios App框架(二)
下一节:错误提示页面

你可能感兴趣的:(从零开始设计搭建ios App框架(三))