1. 程式人生 > >iOS開發 提示框UIAlertController的略微封裝

iOS開發 提示框UIAlertController的略微封裝

之前寫的程式碼,把UIAlertView的封裝剔除之後,發現UIAlertController 封裝的意義不是很大了,畢竟蘋果公司封裝的已經夠好了,好了,上程式碼

//
//  XSDAlertViewTools.h
//  XSDSH  提示框工具類
//
//  Created by 小廣 on 16/1/11.
//  Copyright © 2016年 XSD. All rights reserved.
//

#import <Foundation/Foundation.h>

#define cancelIndex    (-1)

typedef void(^AlertViewBlock)(NSInteger buttonTag);

@interface XSDAlertViewTools : NSObject

+ (XSDAlertViewTools *)shareInstance;

/**
 *  建立提示框
 *
 *  @param title        標題
 *  @param message      提示內容
 *  @param cancelTitle  取消按鈕(無操作,為nil則只顯示一個按鈕)
 *  @param titleArray   標題字串陣列(為nil,預設為"確定")
 *  @param vc           VC iOS8及其以後會用到
 *  @param confirm      點選按鈕的回撥(取消按鈕的Index是cancelIndex -1)
 */
- (void)showAlert:(NSString *)title
          message:(NSString *)message
      cancelTitle:(NSString *)cancelTitle
       titleArray:(NSArray *)titleArray
   viewController:(UIViewController *)vc
          confirm:(AlertViewBlock)confirm;

/**
 *  建立提示框(可變引數版)
 *
 *  @param title        標題
 *  @param message      提示內容
 *  @param cancelTitle  取消按鈕(無操作,為nil則只顯示一個按鈕)
 *  @param vc           VC iOS8及其以後會用到
 *  @param confirm      點選按鈕的回撥(取消按鈕的Index是cancelIndex -1)
 *  @param buttonTitles 按鈕(為nil,預設為"確定",傳引數時必須以nil結尾,否則會崩潰)
 */
- (void)showAlert:(NSString *)title
          message:(NSString *)message
      cancelTitle:(NSString *)cancelTitle
   viewController:(UIViewController *)vc
          confirm:(AlertViewBlock)confirm
     buttonTitles:(NSString *)buttonTitles, ... NS_REQUIRES_NIL_TERMINATION;

/**
 *  建立選單(Sheet)
 *
 *  @param title        標題
 *  @param message      提示內容
 *  @param cancelTitle  取消按鈕(無操作,為nil則只顯示一個按鈕)
 *  @param titleArray   標題字串陣列(為nil,預設為"確定")
 *  @param vc           VC iOS8及其以後會用到
 *  @param confirm      點選確認按鈕的回撥(取消按鈕的Index是cancelIndex -1)
 */
- (void)showSheet:(NSString *)title
          message:(NSString *)message
      cancelTitle:(NSString *)cancelTitle
       titleArray:(NSArray *)titleArray
   viewController:(UIViewController *)vc
          confirm:(AlertViewBlock)confirm;

/**
 *  建立選單(Sheet 可變引數版)
 *
 *  @param title        標題
 *  @param message      提示內容
 *  @param cancelTitle  取消按鈕(無操作,為nil則只顯示一個按鈕)
 *  @param vc           VC
 *  @param confirm      點選按鈕的回撥(取消按鈕的Index是cancelIndex -1)
 *  @param buttonTitles 按鈕(為nil,預設為"確定",傳引數時必須以nil結尾,否則會崩潰)
 */
- (void)showSheet:(NSString *)title
          message:(NSString *)message
      cancelTitle:(NSString *)cancelTitle
   viewController:(UIViewController *)vc
          confirm:(AlertViewBlock)confirm
     buttonTitles:(NSString *)buttonTitles, ... NS_REQUIRES_NIL_TERMINATION ;



@end

.m裡
//
//  XSDAlertViewTools.m
//  XSDSH  提示框工具類
//
//  Created by 小廣 on 16/1/11.
//  Copyright © 2016年 XSD. All rights reserved.
//

#import "XSDAlertViewTools.h"

#define RootVC  [[UIApplication sharedApplication] keyWindow].rootViewController

@interface XSDAlertViewTools ()

@property (nonatomic, copy) AlertViewBlock block;

@end

@implementation XSDAlertViewTools

#pragma mark - 對外方法
+ (XSDAlertViewTools *)shareInstance {
    static XSDAlertViewTools *tools = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tools = [[self alloc] init];
    });
    return tools;
}

/**
 *  建立提示框
 *
 *  @param title        標題
 *  @param message      提示內容
 *  @param cancelTitle  取消按鈕(無操作,為nil則只顯示一個按鈕)
 *  @param titleArray   標題字串陣列(為nil,預設為"確定")
 *  @param vc           VC
 *  @param confirm      點選確認按鈕的回撥
 */
- (void)showAlert:(NSString *)title
          message:(NSString *)message
      cancelTitle:(NSString *)cancelTitle
       titleArray:(NSArray *)titleArray
   viewController:(UIViewController *)vc
          confirm:(AlertViewBlock)confirm {
    //
    if (!vc) vc = RootVC;
    
    [self p_showAlertController:title message:message
                    cancelTitle:cancelTitle titleArray:titleArray
                 viewController:vc confirm:^(NSInteger buttonTag) {
        if (confirm)confirm(buttonTag);
    }];
}


/**
 *  建立提示框(可變引數版)
 *
 *  @param title        標題
 *  @param message      提示內容
 *  @param cancelTitle  取消按鈕(無操作,為nil則只顯示一個按鈕)
 *  @param vc           VC
 *  @param confirm      點選按鈕的回撥
 *  @param buttonTitles 按鈕(為nil,預設為"確定",傳引數時必須以nil結尾,否則會崩潰)
 */
- (void)showAlert:(NSString *)title
          message:(NSString *)message
      cancelTitle:(NSString *)cancelTitle
   viewController:(UIViewController *)vc
          confirm:(AlertViewBlock)confirm
     buttonTitles:(NSString *)buttonTitles, ... NS_REQUIRES_NIL_TERMINATION {
    
    // 讀取可變引數裡面的titles陣列
    NSMutableArray *titleArray = [[NSMutableArray alloc] initWithCapacity:0];
    va_list list;
    if(buttonTitles) {
        //1.取得第一個引數的值(即是buttonTitles)
        [titleArray addObject:buttonTitles];
        //2.從第2個引數開始,依此取得所有引數的值
        NSString *otherTitle;
        va_start(list, buttonTitles);
        while ((otherTitle = va_arg(list, NSString*))) {
            [titleArray addObject:otherTitle];
        }
        va_end(list);
    }
    
    if (!vc) vc = RootVC;

    [self p_showAlertController:title message:message
                    cancelTitle:cancelTitle titleArray:titleArray
                 viewController:vc confirm:^(NSInteger buttonTag) {
        if (confirm)confirm(buttonTag);
    }];
    
}


/**
 *  建立選單(Sheet)
 *
 *  @param title        標題
 *  @param message      提示內容
 *  @param cancelTitle  取消按鈕(無操作,為nil則只顯示一個按鈕)
 *  @param titleArray   標題字串陣列(為nil,預設為"確定")
 *  @param vc           VC
 *  @param confirm      點選確認按鈕的回撥
 */
- (void)showSheet:(NSString *)title
          message:(NSString *)message
      cancelTitle:(NSString *)cancelTitle
       titleArray:(NSArray *)titleArray
   viewController:(UIViewController *)vc
          confirm:(AlertViewBlock)confirm {
    
    if (!vc) vc = RootVC;
    
    [self p_showSheetAlertController:title message:message cancelTitle:cancelTitle
                          titleArray:titleArray viewController:vc confirm:^(NSInteger buttonTag) {
        if (confirm)confirm(buttonTag);
    }];
}

/**
 *  建立選單(Sheet 可變引數版)
 *
 *  @param title        標題
 *  @param message      提示內容
 *  @param cancelTitle  取消按鈕(無操作,為nil則只顯示一個按鈕)
 *  @param vc           VC iOS8及其以後會用到
 *  @param confirm      點選按鈕的回撥
 *  @param buttonTitles 按鈕(為nil,預設為"確定",傳引數時必須以nil結尾,否則會崩潰)
 */
- (void)showSheet:(NSString *)title
          message:(NSString *)message
      cancelTitle:(NSString *)cancelTitle
   viewController:(UIViewController *)vc
          confirm:(AlertViewBlock)confirm
     buttonTitles:(NSString *)buttonTitles, ... NS_REQUIRES_NIL_TERMINATION {
    // 讀取可變引數裡面的titles陣列
    NSMutableArray *titleArray = [[NSMutableArray alloc] initWithCapacity:0];
    va_list list;
    if(buttonTitles) {
        //1.取得第一個引數的值(即是buttonTitles)
        [titleArray addObject:buttonTitles];
        //2.從第2個引數開始,依此取得所有引數的值
        NSString *otherTitle;
        va_start(list, buttonTitles);
        while ((otherTitle= va_arg(list, NSString*))) {
            [titleArray addObject:otherTitle];
        }
        va_end(list);
    }
    
    if (!vc) vc = RootVC;
    
    // 顯示選單提示框
    [self p_showSheetAlertController:title message:message cancelTitle:cancelTitle
                          titleArray:titleArray viewController:vc confirm:^(NSInteger buttonTag) {
                              if (confirm)confirm(buttonTag);
                          }];
    
}


#pragma mark - ----------------內部方法------------------

//UIAlertController(iOS8及其以後)
- (void)p_showAlertController:(NSString *)title
                      message:(NSString *)message
                  cancelTitle:(NSString *)cancelTitle
                   titleArray:(NSArray *)titleArray
               viewController:(UIViewController *)vc
              confirm:(AlertViewBlock)confirm {
    
    UIAlertController  *alert = [UIAlertController alertControllerWithTitle:title
                                                                    message:message
                                                             preferredStyle:UIAlertControllerStyleAlert];
    // 下面兩行程式碼 是修改 title顏色和字型的程式碼
//    NSAttributedString *attributedMessage = [[NSAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f], NSForegroundColorAttributeName:UIColorFrom16RGB(0x334455)}];
//    [alert setValue:attributedMessage forKey:@"attributedTitle"];
    if (cancelTitle) {
        // 取消
        UIAlertAction  *cancelAction = [UIAlertAction actionWithTitle:cancelTitle
                                                                style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * _Nonnull action) {
                                                                  if (confirm)confirm(cancelIndex);
                                                              }];
        [alert addAction:cancelAction];
    }
    // 確定操作
    if (!titleArray || titleArray.count == 0) {
        UIAlertAction  *confirmAction = [UIAlertAction actionWithTitle:@"確定"
                                                                 style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction * _Nonnull action) {
                                                                   if (confirm)confirm(0);
                                                               }];
        
        [alert addAction:confirmAction];
    } else {
        
        for (NSInteger i = 0; i<titleArray.count; i++) {
            UIAlertAction  *action = [UIAlertAction actionWithTitle:titleArray[i]
                                                                     style:UIAlertActionStyleDefault
                                                                   handler:^(UIAlertAction * _Nonnull action) {
                                                                       if (confirm)confirm(i);
                                                                   }];
            // [action setValue:UIColorFrom16RGB(0x00AE08) forKey:@"titleTextColor"]; // 此程式碼 可以修改按鈕顏色
            [alert addAction:action];
        }
    }
    
    [vc presentViewController:alert animated:YES completion:nil];
    
}


// ActionSheet的封裝
- (void)p_showSheetAlertController:(NSString *)title
                           message:(NSString *)message
                       cancelTitle:(NSString *)cancelTitle
                        titleArray:(NSArray *)titleArray
                    viewController:(UIViewController *)vc
                           confirm:(AlertViewBlock)confirm {
    
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:title
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    if (!cancelTitle) cancelTitle = @"取消";
    // 取消
    UIAlertAction  *cancelAction = [UIAlertAction actionWithTitle:cancelTitle
                                                            style:UIAlertActionStyleCancel
                                                          handler:^(UIAlertAction * _Nonnull action) {
                                                              if (confirm)confirm(cancelIndex);
                                                          }];
    [sheet addAction:cancelAction];
    
    if (titleArray.count > 0) {
        for (NSInteger i = 0; i<titleArray.count; i++) {
            UIAlertAction  *action = [UIAlertAction actionWithTitle:titleArray[i]
                                                              style:UIAlertActionStyleDefault
                                                            handler:^(UIAlertAction * _Nonnull action) {
                                                                if (confirm)confirm(i);
                                                            }];
            [sheet addAction:action];
        }
    }
    
    [vc presentViewController:sheet animated:YES completion:nil];
}


@end

用法:
 // 通常的alert
    [[XSDAlertViewTools shareInstance] showAlert:@"提示"
                                         message:@"這個就是提示的內容了"
                                     cancelTitle:@"取消"
                                      titleArray:@[@"確定"]
                                  viewController:nil
                                         confirm:^(NSInteger buttonTag) {
                                             // cancel按鈕的index(buttonTag)是-1 cancelIndex
                                             NSLog(@"點選按鈕的buttonTag===%ld==",(long)buttonTag);
    }];
 // 帶有可變引數的alert
    [[XSDAlertViewTools shareInstance] showAlert:@"提示"
                                         message:@"這個就是提示的內容了"
                                     cancelTitle:@"取消"
                                  viewController:self
                                         confirm:^(NSInteger buttonTag) {
                                             // cancel按鈕的index(buttonTag)是-1 cancelIndex
                                             NSLog(@"點選按鈕的buttonTag===%ld==",(long)buttonTag);
        
    } buttonTitles:@"呼叫",@"檢視",@"我就看看", nil];

如圖:alert 和sheet

  


比較low,大家湊合著看吧...其中的sheet的用法,和上面的alert一樣;