提示:文章只是用來(lái)記錄本人自己在學(xué)習(xí)過(guò)程中所遇到的一些問(wèn)題的解決方案,如果有什么意見(jiàn)可以留言提出來(lái),不喜勿噴哦!
注:本文轉(zhuǎn)自 “iOS開(kāi)發(fā)筆記(3) -- UIAlertController的二次封裝” 如要查看原文,請(qǐng)點(diǎn)擊進(jìn)入氏堤。
時(shí)間有腳
在我們的項(xiàng)目中,在需要使用到 UIAlertController 時(shí)候搏明,不可能每次都去重新把初始化之類的東西重新寫一次鼠锈,還不如自己進(jìn)行二次封裝成工具類,直接調(diào)用工具類星著,這樣代碼的結(jié)構(gòu)就更加合理购笆。
- AlertToolViewController.h
#import <UIKit/UIKit.h>
@interface AlertToolViewController : UIAlertController
+ (instancetype)initWBAlerControllerWithTitle:(NSString *)title message:(NSString *)message style:(NSString *)style titleArr:(NSMutableArray *)titleArr alerAction:(void (^)(NSInteger index))alerAction;
-(void)showWBAler;
@end
- AlertToolViewController.m
#import "AlertToolViewController.h"
@interface AlertToolViewController ()
@end
@implementation AlertToolViewController
//重寫方法
+ (instancetype)initWBAlerControllerWithTitle:(NSString *)title message:(NSString *)message style:(NSString *)style titleArr:(NSMutableArray *)titleArr alerAction:(void (^)(NSInteger index))alerAction{
//參數(shù)說(shuō)明:
//title表示彈框的標(biāo)題;
//message表示彈框的展示的信息;
//style是0或者1;代表彈框的類型;UIAlertControllerStyleActionSheet = 0,UIAlertControllerStyleAlert = 1;
//titleArr為彈框中出現(xiàn)的按鈕標(biāo)題的數(shù)組;個(gè)數(shù)你自己決定;
//alerAction為block回調(diào)事件,因?yàn)檫@里只需要判斷點(diǎn)擊的按鈕坐標(biāo)就可以,其他需要?dú)ぷ孕刑砑訁?shù);
//判斷彈框類型
if ([style isEqualToString:@"1"]) {
AlertToolViewController *alert = [AlertToolViewController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
for (NSInteger i = 0; i < titleArr.count; i++) {
UIAlertAction *confirm = [UIAlertAction actionWithTitle:[titleArr objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (alerAction) {
alerAction(i);
}
}];
[alert addAction:confirm];
}
return alert;
}else{
AlertToolViewController *alert = [AlertToolViewController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
for (NSInteger i = 0; i < titleArr.count; i++) {
UIAlertAction *confirm = [UIAlertAction actionWithTitle:[titleArr objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (alerAction) {
alerAction(i);
}
}];
[alert addAction:confirm];
}
return alert;
}
}
-(void)showWBAler{
[[self getCurrentVC] presentViewController:self animated:YES completion:nil];
}
/**
得到當(dāng)前控制視圖
@return 當(dāng)前控制視圖
*/
-(UIViewController *)getCurrentVC
{
UIViewController *result = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tempWindow in windows)
{
if (tempWindow.windowLevel == UIWindowLevelNormal)
{
window = tempWindow;
break;
}
}
}
UIView *frontView = [[window subviews] objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
result = nextResponder;
else
result = window.rootViewController;
return result;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- 調(diào)用
- (void)action{
//參數(shù)說(shuō)明:
//style是0或者1;代表彈框的類型;UIAlertControllerStyleActionSheet = 0,UIAlertControllerStyleAlert = 1;
//titleArr為彈框中出現(xiàn)的按鈕標(biāo)題的數(shù)組;個(gè)數(shù)你自己決定;
//alerAction為block回調(diào)事件,因?yàn)檫@里只需要判斷點(diǎn)擊的按鈕坐標(biāo)就可以,其他需要?dú)ぷ孕刑砑訁?shù);
NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"確定",@"取消", nil];
AlertToolViewController *alert =[AlertToolViewController initWBAlerControllerWithTitle:@"Title" message:@"測(cè)試啊測(cè)試啊" style:@"1" titleArr:arr alerAction:^(NSInteger index) {
//這里為點(diǎn)擊事件的回調(diào);
if (index == 0) {
NSLog(@"1");
}
if (index == 1) {
NSLog(@"2");
}
if (index == 2) {
NSLog(@"3");
}
}];
[alert showWBAler];
}