我們寫(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