iOS 封装UIAlertViewController与UIAlertView

在项目中,我们经常会用到alertView。特别是在iOS8之后,采用了UIAlertViewController,每次创建都要写很多重复的代码。为了使用方便将UIAlertViewController与UIAlertView等装成了一个简单的工具类。只需要一句代码就可以实现,采用了UIAlertView的创建方式,非常简单。这里是一个简单的demo。

实现起来很简单:.h文件、

typedef void(^actionBlock)(NSInteger buttonTag);

@interface JCAlertView : NSObject

@property (nonatomic, copy) actionBlock alertBlcok;
@property (nonatomic, copy) actionBlock sheetBlcok;

+ (JCAlertView *)sharedInstance;


/*!
@method 创建提示框
@param viewController     用于显示alert的controller
@param title              标题
@param message            提示内容
@param cancelTitle        取消按钮
@param block              点击按钮的回调
@param otherButtonTitles  按钮
@discussion               取消按钮buttonTag为0,其他按钮按顺序从1开始.                           iOS8之前buttonTag为系统的buttonIndex.如果取消和其他按钮都为nil则没有操作按钮,传参数时必须以nil结尾,否则会崩溃.
*
*/
- (void)showAlertController:(UIViewController *)viewController
                     title:(NSString *)title
                   message:(NSString *)message
               cancelTitle:(NSString *)cancelTitle
               actionBlock:(actionBlock)block
         otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

/*!
@method  创建actionSheet
@param viewController     用于显示actionSheet的controller
@param title              标题
@param message            提示内容
@param cancelTitle        取消按钮
@param block              点击按钮的回调 
@param otherButtonTitles  按钮
@param destructive        destructive按钮
@discussion               取消按钮buttonTag为0,destructive按钮的buttonTag==-1,其他按钮按顺序从1开始. iOS8之前buttonTag为系统的buttonIndex.如果取消和其他按钮都为nil则没有操作按钮,传参数时必须以nil结尾,否则会崩溃.

*
*/
- (void)showActionSheetController:(UIViewController *)viewController
                           title:(NSString *)title
                         message:(NSString *)message
                     cancelTitle:(NSString *)cancelTitle
                     destructive:(NSString *)destructive
                     actionBlock:(actionBlock)block
               otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;



@end

两个方法,一个是创建alertView一个是创建actionSheet,iOS8之前是用的UIalertView,iOS8之后用UIalertViewController。

具体的实现:alertView

- (void)showAlertController:(UIViewController *)viewController
                     title:(NSString *)title
                   message:(NSString *)message
               cancelTitle:(NSString *)cancelTitle
               actionBlock:(actionBlock)block
         otherButtonTitles:(NSString *)otherButtonTitles, ...  {
   
   self.alertBlcok = block;
   // 读取可变参数里面的titles数组
   NSMutableArray *titlesArray = [[NSMutableArray alloc] initWithCapacity:0];
   va_list list;
   if(otherButtonTitles) {
       //1.取得第一个参数的值(即是buttonTitles)
       [titlesArray addObject:otherButtonTitles];
       //2.从第2个参数开始,依此取得所有参数的值
       NSString *otherTitle;
       va_start(list, otherButtonTitles);
       while ((otherTitle = va_arg(list, NSString*))) {
           [titlesArray addObject:otherTitle];
       }
       va_end(list);
   }

   if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0){
       
       [self showAlertController:viewController title:title message:message cancelTilte:cancelTitle otherTitleArray:titlesArray actionBlock:^(NSInteger buttonTag) {
           self.alertBlcok(buttonTag);
       }];
   }else{
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles: nil];
   for(NSString *otherTitle in titlesArray){
       [alert addButtonWithTitle:otherTitle];
   }
       [alert show];
   }

   
}
//alert封装
- (void)showAlertController:(UIViewController *)viewController
                     title:(NSString *)title
                   message:(NSString *)message
               cancelTilte:(NSString *)cancelTitle
           otherTitleArray:(NSArray *)otherTitleArray
               actionBlock:(actionBlock)block {
   
   UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
   
   // 下面两行代码 是修改 title颜色和字体的代码
   /*
    NSAttributedString *attributedMessage = [[NSAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f], NSForegroundColorAttributeName:[UIColor redColor]}];       
    [alert setValue:attributedMessage forKey:@"attributedTitle"];
    */
   if (cancelTitle) {
       // 取消
       UIAlertAction  *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
           block(0);
       }];
       [alert addAction:cancelAction];
   }
   
   if (otherTitleArray.count>0) {
       for (NSInteger i = 0; i

actionSheet的实现:

- (void)showActionSheetController:(UIViewController *)viewController
                            title:(NSString *)title
                          message:(NSString *)message
                      cancelTitle:(NSString *)cancelTitle
                      destructive:(NSString *)destructive
                      actionBlock:(actionBlock)block
                otherButtonTitles:(NSString *)otherButtonTitles, ... {
    self.sheetBlcok = block;
    // 读取可变参数里面的titles数组
    NSMutableArray *titlesArray = [[NSMutableArray alloc] initWithCapacity:0];
    
    va_list list;
    if(otherButtonTitles) {
        //1.取得第一个参数的值(即是buttonTitles)
        [titlesArray addObject:otherButtonTitles];
        //2.从第2个参数开始,依此取得所有参数的值
        NSString *otherTitle;
        va_start(list, otherButtonTitles);
        while ((otherTitle = va_arg(list, NSString*))) {
            [titlesArray addObject:otherTitle];
        }
        va_end(list);
    }

    if([[[UIDevice currentDevice] systemVersion] floatValue]){
        [self showActionSheetController:viewController title:title message:message cancelTitle:cancelTitle destructive:destructive titlesArray:titlesArray actionBlock:^(NSInteger buttonTag) {
            self.sheetBlcok(buttonTag);
        }];
    }
    else{
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:cancelTitle destructiveButtonTitle:destructive otherButtonTitles:nil];
        for(NSString *otherTitle in titlesArray){
            [sheet addButtonWithTitle:otherTitle];
        }
        [sheet showInView:viewController.view];
    }

}
// ActionSheet的封装

- (void)showActionSheetController:(UIViewController *)viewController
                            title:(NSString *)title
                          message:(NSString *)message
                      cancelTitle:(NSString *)cancelTitle
                      destructive:(NSString *)destructive
                      titlesArray:(NSArray *)titlesArray
                      actionBlock:(actionBlock)block{
    
    
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    if (cancelTitle) {
        UIAlertAction  *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            self.sheetBlcok(0);
        }];
        [sheet addAction:cancelAction];
    }
    if (destructive) {
        UIAlertAction  *destructiveAction = [UIAlertAction actionWithTitle:destructive style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            self.sheetBlcok(-1);
        }];
        [sheet addAction:destructiveAction];
    }
    if (titlesArray.count > 0) {
        for (NSInteger i = 0; i

你可能感兴趣的:(iOS 封装UIAlertViewController与UIAlertView)