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<otherTitleArray.count; i++) {
           
           UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherTitleArray[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
               block(i+1);
           }];
           // [action setValue:[UIColor redColor] forKey:@"titleTextColor"]; // 此代码 可以修改按钮颜色
           [alert addAction:otherAction];
       }

   }
   
   [viewController presentViewController:alert animated:YES completion:nil];
   
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
   self.alertBlcok(buttonIndex);
}

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<titlesArray.count; i++) {
            UIAlertAction  *action = [UIAlertAction actionWithTitle:titlesArray[i]
                                                              style:UIAlertActionStyleDefault
                                                            handler:^(UIAlertAction * _Nonnull action) {
                                                                self.sheetBlcok(i+1);
                                                            }];
            [sheet addAction:action];
        }
    }
    [viewController presentViewController:sheet animated:YES completion:nil];
    
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    self.sheetBlcok(buttonIndex);
}



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 其实这几天我有点黔驴技穷。只是牢牢记得各位写作大珈们各种写作分享中提到的坚持去写这一原则。所以每天不知道写什么...
    梨花兼着芭蕉阅读 2,507评论 0 0
  • 关键词选择是SEO排名的重心之一, 下面简单讲讲选取关键词的一些常见方法: 一、从口语入手。比如对于秃顶来说,书面...
    xbxnnn阅读 4,388评论 0 0
  • 我以为,我可以轻轻松松提出分手,这就是现在的我,没有留恋,也没有羁绊。但是却发现,只是说得出,却再也做不回那个最初...
    总会一个人阅读 2,615评论 0 1
  • 八月,阳光明媚,光阴撒向疲惫的身子,路上的人儿拖着重重的影子,困顿的打着哈欠,悠然的像个国王。如此,甚好。 八月,...
    雪落砚阅读 3,790评论 1 7
  • 年少的轻狂,迟暮的伤。“原来是你给了我自负的胆量,还你”。这是我准备好久的潜台词,想把它歇斯底里的还给你,但是想着...
    L3687阅读 5,822评论 0 1

友情链接更多精彩内容