LGLAlertView 提示框

来源:http://www.cnblogs.com/ljmaque/archive/2016/09/30/LGLAlertView.html
-Advertisement-
Play Games

使用與iOS8 以後,只是把系統的UIAlertController進行了封裝,省的每次用的時候要寫很多的代碼。封裝後只需要一句代碼即可 , deome 地址:https://github.com/liguoliangiOS/LGLAlertView.git 上代碼LGLAlertView.h: L ...


使用與iOS8 以後,只是把系統的UIAlertController進行了封裝,省的每次用的時候要寫很多的代碼。封裝後只需要一句代碼即可 , deome 地址:https://github.com/liguoliangiOS/LGLAlertView.git

上代碼LGLAlertView.h:

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

 

 

typedef NS_ENUM(NSInteger, LGLAlertViewActionStyle) {

    LGLAlertViewActionStyleDefault = 0,

    LGLAlertViewActionStyleCancel,

    LGLAlertViewActionStyleDestructive

};

 

/** alertView的回調block */

 

typedef void (^CallBackBlock)(NSInteger btnIndex);

 

/** alertView的回調block */

 

typedef void (^TextFieldCallBackBlock)(NSString * text);

 

 

@interface LGLAlertView : NSObject

 

/**

 * 單個或者沒有按鈕 不執行任何操作 只是提示總用

 * @param title    提示的標題

 * @param message  提示信息

 * @param btnTitle 單個按鈕的標題名稱

 *

 */

+ (void)showAlertViewWith:(UIViewController *)viewController title:(NSString *)title

            message:(NSString *)message buttonTitle:(NSString *)btnTitle

              buttonStyle:(LGLAlertViewActionStyle)buttonStyle;

 

/**

 * 有兩個或者多個按鈕 確定 取消

 * @param title             提示的標題

 * @param message           提示信息

 * @param btnTitle          單個按鈕的標題名稱

 * @param cancelButtonTitle 取消按鈕

 * @param destructiveBtn    destructiveBtn按鈕

 * @param otherButtonTitles 確定按鈕

 */

+ (void)showAlertViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message

            CallBackBlock:(CallBackBlock)textBlock cancelButtonTitle:(NSString *)cancelBtnTitle

                destructiveButtonTitle:(NSString *)destructiveBtnTitle

                    otherButtonTitles:(NSString *)otherBtnTitles,...NS_REQUIRES_NIL_TERMINATION;

 

 

/**

 * 有輸入框  確定 取消 (註: 這裡只做了有一個輸入框)

 * @param title             提示的標題

 * @param message           提示信息

 * @param btnTitle          單個按鈕的標題名稱

 * @param cancelButtonTitle 取消按鈕

 * @param destructiveBtn    destructiveBtn按鈕

 * @param otherButtonTitles 確定按鈕

 */

 

+ (void)showAlertTextFieldViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message

            TextFeildCallBackBlock:(TextFieldCallBackBlock)block cancelButtonTitle:(NSString *)cancelBtnTitle

                otherButtonTitles:(NSString *)otherBtnTitle;

 

/**

 * 單個或者沒有按鈕ActionSheet 僅僅只是提示作用 按鈕沒有響應事件

 * @param title    提示的標題

 * @param message  提示信息

 * @param btnTitle 單個按鈕的標題名稱

 *

 */

+ (void)showAlertActionSheetViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message

              buttonTitle:(NSString *)btnTitle buttonStyle:(LGLAlertViewActionStyle)buttonStyle;

 

/**

 * 沒有按鈕ActionSheet 按鈕有響應事件

 * @param title    提示的標題

 * @param message  提示信息

 * @param btnTitle 單個按鈕的標題名稱

 *

 */

+ (void)showAlertActionSheetWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message

              callbackBlock:(CallBackBlock)block destructiveButtonTitle:(NSString *)destructiveBtnTitle

                cancelButtonTitle:(NSString *)cancelBtnTitle

                    otherButtonTitles:(NSString *)otherBtnTitles, ...NS_REQUIRES_NIL_TERMINATION;

@end

 

LGLAlertView.m:文件

#import "LGLAlertView.h"
#define  LGLAlertShowTime 1.0

@implementation LGLAlertView

// ======================================================================== ----- AlertView start----- ==================================================================================

// 單個或沒有按鈕

+ (void)showAlertViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message buttonTitle:(NSString *)btnTitle buttonStyle:(LGLAlertViewActionStyle)buttonStyle {
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if (btnTitle.length) {
        UIAlertActionStyle actionStyle = (buttonStyle == LGLAlertViewActionStyleDefault) ? UIAlertActionStyleDefault : ((buttonStyle ==  LGLAlertViewActionStyleCancel) ? UIAlertActionStyleCancel : UIAlertActionStyleDestructive);
        UIAlertAction * alertAction = alertAction = [UIAlertAction actionWithTitle:btnTitle style:actionStyle handler:^(UIAlertAction * _Nonnull action) {
            [self performSelector:@selector(dismissAlertController:) withObject:alertController afterDelay:LGLAlertShowTime];
        }];;
        [alertController addAction:alertAction];
        [viewController presentViewController:alertController animated:YES completion:nil];
    } else {
        [viewController presentViewController:alertController animated:YES completion:nil];
        [self performSelector:@selector(dismissAlertController:) withObject:alertController afterDelay:LGLAlertShowTime];
    }
}

// 單個或多個按鈕

+ (void)showAlertViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message CallBackBlock:(CallBackBlock)block cancelButtonTitle:(NSString *)cancelBtnTitle destructiveButtonTitle:(NSString *)destructiveBtnTitle otherButtonTitles:(NSString *)otherBtnTitles,... {
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    //添加按鈕
    if (cancelBtnTitle.length) {
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            block(0);
        }];
        [alertController addAction:cancelAction];
    }
    if (destructiveBtnTitle.length) {
        UIAlertAction * destructiveAction = [UIAlertAction actionWithTitle:destructiveBtnTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            block(1);
        }];
        [alertController addAction:destructiveAction];
    }
    if (otherBtnTitles.length) {
        UIAlertAction *otherActions = [UIAlertAction actionWithTitle:otherBtnTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            (!cancelBtnTitle.length && !destructiveBtnTitle.length) ? block(0) : (((cancelBtnTitle.length && !destructiveBtnTitle.length) || (!cancelBtnTitle.length && destructiveBtnTitle.length)) ? block(1) : block(2));
        }];
        [alertController addAction:otherActions];
        
        va_list args;
        va_start(args, otherBtnTitles);
        if (otherBtnTitles.length) {
            NSString * otherString;
            int index = 2;
            (!cancelBtnTitle.length && !destructiveBtnTitle.length) ? (index = 0) : ((cancelBtnTitle.length && !destructiveBtnTitle.length) || (!cancelBtnTitle.length && destructiveBtnTitle.length) ? (index = 1) : (index = 2));
            while ((otherString = va_arg(args, NSString*))) {
                index ++ ;
                UIAlertAction * otherActions = [UIAlertAction actionWithTitle:otherString style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    block(index);
                }];
                [alertController addAction:otherActions];
            }
        }
        va_end(args);
    }
    [viewController presentViewController:alertController animated:YES completion:nil];
    
    //如果沒有按鈕,自動延遲消失
    if (!cancelBtnTitle.length && !destructiveBtnTitle.length && !otherBtnTitles) {
        //此時self指本類
        [self performSelector:@selector(dismissAlertController:) withObject:alertController afterDelay:LGLAlertShowTime];
    }
}

// 有輸入框

+ (void)showAlertTextFieldViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message TextFeildCallBackBlock:(TextFieldCallBackBlock)textBlock cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonTitles:(NSString *)otherBtnTitle {
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
     }];
    if (cancelBtnTitle.length) {
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            [self dismissAlertController:alertController];
        }];
        [alertController addAction:cancelAction];
    }
    if (otherBtnTitle.length) {
        UIAlertAction * otherAction = [UIAlertAction actionWithTitle:otherBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            textBlock([alertController.textFields firstObject].text);
        }];
        [alertController addAction:otherAction];
    }
    [viewController presentViewController:alertController animated:YES completion:nil];
    //如果沒有按鈕,自動延遲消失
    if (!cancelBtnTitle.length && !otherBtnTitle.length) {
        //此時self指本類
        [self performSelector:@selector(dismissAlertController:) withObject:alertController afterDelay:LGLAlertShowTime];
    }

}
// ======================================================================== ----- AlertView end----- ==================================================================================
#pragma mark ==== 點擊事件 ======
+ (void)dismissAlertController:(UIAlertController *)alert {
    [alert dismissViewControllerAnimated:YES completion:nil];
}

// ======================================================================== -- ActionSheet Start -- ====================================================================================

+ (void)showAlertActionSheetViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message buttonTitle:(NSString *)btnTitle buttonStyle:(LGLAlertViewActionStyle)buttonStyle {

     UIAlertController * actionController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    if (btnTitle.length) {
        UIAlertActionStyle actionStyle = (buttonStyle == LGLAlertViewActionStyleDefault) ? UIAlertActionStyleDefault : ((buttonStyle ==  LGLAlertViewActionStyleCancel) ? UIAlertActionStyleCancel : UIAlertActionStyleDestructive);
        UIAlertAction * alertAction = alertAction = [UIAlertAction actionWithTitle:btnTitle style:actionStyle handler:^(UIAlertAction * _Nonnull action) {
            [self performSelector:@selector(dismissAlertController:) withObject:actionController afterDelay:LGLAlertShowTime];
        }];;
        [actionController addAction:alertAction];
        [viewController presentViewController:actionController animated:YES completion:nil];
    } else {
        [viewController presentViewController:actionController animated:YES completion:nil];
    //如果沒有按鈕,自動延遲消失
        [self performSelector:@selector(dismissAlertController:) withObject:actionController afterDelay:LGLAlertShowTime];
    }
}

+ (void)showAlertActionSheetWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message callbackBlock:(CallBackBlock)block destructiveButtonTitle:(NSString *)destructiveBtnTitle cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonTitles:(NSString *)otherBtnTitles, ... {
    
    UIAlertController * actionController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    //添加按鈕
    if (destructiveBtnTitle.length) {
        UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveBtnTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            block(0);
        }];
        [actionController addAction:destructiveAction];
    }
    if (cancelBtnTitle.length) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            destructiveBtnTitle.length ? block(1) : block(0);
        }];
        [actionController addAction:cancelAction];
    }
    if (otherBtnTitles.length)
    {
        UIAlertAction *otherActions = [UIAlertAction actionWithTitle:otherBtnTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            (!cancelBtnTitle.length && !destructiveBtnTitle.length) ? block(0) : (((destructiveBtnTitle.length && !cancelBtnTitle.length) || (!destructiveBtnTitle.length && cancelBtnTitle.length)) ? block(1) : block(2));
        }];
        [actionController addAction:otherActions];
        
        
        va_list args;
        va_start(args, otherBtnTitles);
        if (otherBtnTitles.length) {
            NSString * otherString;
            int index = 2;
            (!cancelBtnTitle.length && !destructiveBtnTitle.length) ? (index = 0) : ((cancelBtnTitle.length && !destructiveBtnTitle.length) || (!cancelBtnTitle.length && destructiveBtnTitle.length) ? (index = 1) : (index = 2));
            while ((otherString = va_arg(args, NSString*))) {
                index ++ ;
                UIAlertAction * otherActions = [UIAlertAction actionWithTitle:otherString style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    block(index);
                }];
                [actionController addAction:otherActions];
            }
        }
        va_end(args);
    }
    [viewController presentViewController:actionController animated:YES completion:nil];
    
    //如果沒有按鈕,自動延遲消失
    if (!cancelBtnTitle.length && !destructiveBtnTitle.length && !otherBtnTitles.length) {
        //此時self指本類
        [self performSelector:@selector(dismissAlertController:) withObject:actionController afterDelay:LGLAlertShowTime];
    }
    
}
@end

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Ext.window.MessageBox是一個工具類,他繼承自Ext.window.Windoe對象,用來生成各種風格的信息提示對話框,其實例對象可以通過Ext.MessageBox或Ext.Msg進行訪問,使用Ext.MessageBox和使用Ext.Msg有相同的效果,而後者提供了更簡短的調用 ...
  • GCD即為Grand Central Dispatch的縮寫,是一種主要用於非同步處理任務的安全的高性能解決方案。 在此不對比其他非同步處理技術,只記錄GCD的使用及說明。 先記錄一些必要的概念:線程,同步,非同步,並行隊列,串列隊列 線程:程式中任務執行流的最小單元。一個應用程式,一般存在一個進程(擁有 ...
  • GPUIMAGE中GPUImageStillCamera可以調用系統相機,並實現實時濾鏡,但是我沒有找到相機全屏的方法,望知道的說一下 GPUImageStillCamera繼承自GPUImageVideoCamera類,添加了捕獲照片的功能。 GPUImageVideoCamera 初始化方法: ...
  • 本文實現全自定義控制項--自定義開關 本文地址:http://www.cnblogs.com/wuyudong/p/5922316.html,轉載請註明源地址。 自定義開關 (View),本文完成下麵內容 1. 寫個類繼承View2. 拷貝包含包名的全路徑到xml中3. 界面中找到該控制項, 設置初始信 ...
  • Android Studio增量升級什麼情況下使用最合適呢? 比如現在的as版本是2.2版本,而你的as版本2.0版本,這個時候點Check For Updates就沒有反應了,因為你已經2個有版本沒升級了(版本跨度太大 ),所以這樣就不能線上升級了。 重要的信息 谷歌更新地址:https://dl ...
  • 最近在看推送方面的知識,用的是信鴿推送主要是因為後臺用的是信鴿 推送用第三方推送,也就是在客戶端建一個廣播接收器,當伺服器發送消息時發送到信鴿,信鴿再發送一次,廣播接受器接受下; 我實現的功能比較簡單,當app在運行狀態時,會在主頁展示一個彈窗展示推送的消息;如果app不在運行狀態且service沒 ...
  • 作為一個碼農,什麼都不用多講,貼代碼 build project build module Application public class App extends Application { private AppComponent appComponent; @Override public v ...
  • 首先要是在真機上查看android資料庫文件必須要取得root許可權,這裡怎麼獲取就不在多說。可能是手機的原因,本人獲取到的手機許可權如下: 雖然data目錄下的許可權已經開放,可是data下的包許可權卻未獲取到write許可權,基於這種情況解決方法如下: 通過命令行獲取到該包的許可權,可以看到目前該包許可權已經 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...