給UIAlertController添加一個(gè)分類(lèi)

我們寫(xiě)代碼時(shí)候,經(jīng)常會(huì)用到彈框,然后到處都是寫(xiě)基本一樣的代碼,干脆給它做一個(gè)分類(lèi),封裝起來(lái),這樣,直接調(diào)用方法。我這里僅僅提供了四種最常見(jiàn)的。使用方法非常簡(jiǎn)單,直接將UIViewController+Message 這個(gè)類(lèi)拖入到項(xiàng)目中,導(dǎo)入頭文件需要使用的地方,建議以后直接放pch
Dome: https://github.com/LYWGod/UIAlertControllerExtension

第一種:彈出UIAlertController 風(fēng)格為 UIAlertControllerStyleAlert 且有取消和確定按鈕


Snip20161124_2.png

第二種:彈出UIAlertController 風(fēng)格為UIAlertControllerStyleAlert 并且只有一個(gè)確定按鈕


Snip20161124_3.png

第三種:彈出UIAlertController 風(fēng)格為UIAlertControllerStyleActionSheet并且有兩個(gè)選擇項(xiàng)和一個(gè)取消項(xiàng)


Snip20161124_4.png

第三種:彈出UIAlertController 風(fēng)格為UIAlertControllerStyleActionSheet并且有一個(gè)選擇項(xiàng)和一個(gè)取消項(xiàng)


Snip20161124_5.png

封裝的方法非常簡(jiǎn)單。如果需要也可以直接下載
代碼.h文件

#import <UIKit/UIKit.h>

@interface UIViewController (Message)

/**
 彈出UIAlertController
 
 @param title   標(biāo)題
 @param message 消息
 @param sure    點(diǎn)擊確定按鈕
 */
- (void)showAlertSureWithTitle:(NSString *)title message:(NSString *)message sure:(void (^) (UIAlertAction *action))sure;

/**
 彈出UIAlerController
 
 @param title   標(biāo)題
 @param message 消息
 @param sure    點(diǎn)擊確定
 @param cancel  點(diǎn)擊取消
 */
- (void)showAlertSureAndCancelWithTitle:(NSString *)title message:(NSString *)message sure:(void (^) (UIAlertAction *action))sure cancel:(void (^) (UIAlertAction *action))cancel;


/**
 彈出UIAlertController

 @param actionOneTitle 標(biāo)題
 @param handlerOne     點(diǎn)擊標(biāo)題的事件
 */
- (void)showSheetOneaction:(NSString *)actionOneTitle handlerOne:(void(^)(UIAlertAction *action))handlerOne;

/**
 彈出UIAlerController 
 
 @param actionOneTitle 第一標(biāo)題
 @param actionTwoTitle 第二個(gè)標(biāo)題
 @param handlerOne     第一個(gè)標(biāo)題點(diǎn)擊事件
 @param handlerTwo     第二個(gè)標(biāo)題點(diǎn)擊事件
 */
- (void)showSheetTwoaction:(NSString *)actionOneTitle actionTwo:(NSString *)actionTwoTitle handlerOne:(void(^)(UIAlertAction *action))handlerOne handlerTwo:(void (^) (UIAlertAction *action))handlerTwo;

@end

.m文件

#import "UIViewController+Message.h"

@implementation UIViewController (Message)


/**
 彈出UIAlertController

 @param title   標(biāo)題
 @param message 消息
 @param sure    點(diǎn)擊確定按鈕
 */
- (void)showAlertSureWithTitle:(NSString *)title message:(NSString *)message sure:(void (^) (UIAlertAction *action))sure;
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:sure];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}



/**
 彈出UIAlerController

 @param title   標(biāo)題
 @param message 消息
 @param sure    點(diǎn)擊確定
 @param cancel  點(diǎn)擊取消
 */
- (void)showAlertSureAndCancelWithTitle:(NSString *)title message:(NSString *)message sure:(void (^) (UIAlertAction *action))sure cancel:(void (^) (UIAlertAction *action))cancel
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:sure];
    
    UIAlertAction *revoke = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:cancel];
    
    [alert addAction:action];
    [alert addAction:revoke];
    
    [self presentViewController:alert animated:YES completion:nil];
}

/**
 彈出UIAlertController
 
 @param actionOneTitle 標(biāo)題
 @param handlerOne     點(diǎn)擊標(biāo)題的事件
 */
- (void)showSheetOneaction:(NSString *)actionOneTitle handlerOne:(void(^)(UIAlertAction *action))handlerOne
{
    UIAlertController *alertSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *actionOne = [UIAlertAction actionWithTitle:actionOneTitle style:UIAlertActionStyleDefault handler:handlerOne];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertSheet addAction:actionOne];
    [alertSheet addAction:cancelAction];
    
    [self presentViewController:alertSheet animated:YES completion:nil];
}


/**
 彈出UIAlerController

 @param actionOneTitle 第一標(biāo)題
 @param actionTwoTitle 第二個(gè)標(biāo)題
 @param handlerOne     第一個(gè)標(biāo)題點(diǎn)擊事件
 @param handlerTwo     第二個(gè)標(biāo)題點(diǎn)擊事件
 */
- (void)showSheetTwoaction:(NSString *)actionOneTitle actionTwo:(NSString *)actionTwoTitle handlerOne:(void(^)(UIAlertAction *action))handlerOne handlerTwo:(void (^) (UIAlertAction *action))handlerTwo
{
    UIAlertController *alertSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *actionOne = [UIAlertAction actionWithTitle:actionOneTitle style:UIAlertActionStyleDefault handler:handlerOne];
    
    UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:actionTwoTitle style:UIAlertActionStyleDefault handler:handlerTwo];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertSheet addAction:actionOne];
    [alertSheet addAction:actionTwo];
    [alertSheet addAction:cancelAction];
    
    [self presentViewController:alertSheet animated:YES completion:nil];
}

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,838評(píng)論 4 61
  • 紅榜: 1. 今天在時(shí)間安排上有進(jìn)步,主要是實(shí)現(xiàn)了資料整理--巡回檢查--現(xiàn)場(chǎng)設(shè)備結(jié)構(gòu)學(xué)習(xí)三者的合理安排。 資料整...
    Inker閱讀 362評(píng)論 1 2

友情鏈接更多精彩內(nèi)容