iOS9開始UIAlertView不被推薦使用滓玖,取而代之的是iOS8推出的UIAlertController详拙。
從它們的命名就能看得出一個是繼承了UIView,一個是繼承了UIViewController遍尺。不少小伙伴看到這個變動后當(dāng)場就不開心了截酷。以前的UIAlertView到處可以調(diào)用,并且宏定義起來調(diào)用也非常方便乾戏。
#define Alert(msg) \
[[[UIAlertView alloc] initWithTitle:@"" \
message:msg \
delegate:nil \
cancelButtonTitle:@"確定" \
otherButtonTitles:nil, nil] show]; \
可是現(xiàn)在的UIAlertController是需要通過presentViewController來彈窗的,所以必須要拿到Controller才能調(diào)用合搅,這樣很不方便多搀。讓我們來看看有沒有好的方法來改造一下呢。
要不來點(diǎn)簡單粗暴的吧~
首先最最關(guān)鍵的就是獲取當(dāng)前顯示的ViewController灾部,也是頂層的ViewController康铭。
#pragma mark - Utils
- (UIViewController *)getCurrentViewController
{
UIViewController *currentViewController = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if ([window subviews].count == 0) {
return nil;
}
if (window.windowLevel != UIWindowLevelNormal) {
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * subWindow in windows)
{
if (subWindow.windowLevel == UIWindowLevelNormal) {
window = subWindow;
break;
}
}
}
UIView *frontView = [[window subviews] objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
currentViewController = nextResponder;
} else {
currentViewController = window.rootViewController;
}
return currentViewController;
}
獲取到當(dāng)前的ViewController后,就可以配置UIAlertController進(jìn)行跳轉(zhuǎn)赌髓,方法命名模仿UIAlertView,并且用Block代替了UIAlertDelegate中的alertView:clickedButtonAtIndex:代理方法从藤。
#pragma mark -- base method
/**
* 提示框基礎(chǔ)方法
*
* @param title 標(biāo)題
* @param message 消息
* @param cancelButtonTitle 取消按鈕標(biāo)題
* @param cancelCallBack 取消按鈕回調(diào)
* @param otherCallBack 其他按鈕回調(diào)
* @param otherButtonTitles 其他按鈕
*/
- (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle cancelCallBack:(CancelCallBack)cancelCallBack otherCallBack:(OtherCallBack)otherCallBack otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION{
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
//初始化AlertController
UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
//定義va_list
va_list argsList;
//指向首地址
va_start(argsList, otherButtonTitles);
NSInteger index = 0;
//遍歷
while (otherButtonTitles) {
//過濾入?yún)㈩愋? if (![otherButtonTitles isKindOfClass:[NSString class]]) {
break;
}
//過濾空字符串
if (![self isBlankString:otherButtonTitles]) {
//添加提示框按鈕動作
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:otherButtonTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (otherCallBack) {
[alertViewController dismissViewControllerAnimated:YES completion:nil];
otherCallBack(index);
}
}];
[alertViewController addAction:alertAction];
}
//指向下一個地址
otherButtonTitles = va_arg(argsList, NSString *);
index++;
}
va_end(argsList);
//取消按鈕
if (![self isBlankString:cancelButtonTitle]) {
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//點(diǎn)擊過后關(guān)閉提示框
if (cancelCallBack) {
[alertViewController dismissViewControllerAnimated:YES completion:nil];
//取消回調(diào)方法
cancelCallBack();
}
}];
[alertViewController addAction:alertAction];
}
//從當(dāng)前控制器中模態(tài)彈出提示框
[[self getCurrentViewController] presentViewController:alertViewController animated:YES completion:nil];
#else
self.cancelCallBack = [cancelCallBack copy];
self.otherCallBack = [otherCallBack copy];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
va_list argsList;
va_start(argsList, otherButtonTitles);
NSInteger index = 0;
while (otherButtonTitles) {
if (![self isBlankString:otherButtonTitles]) {
[alertView addButtonWithTitle:otherButtonTitles];
}
otherButtonTitles = va_arg(argsList, NSString *);
index++;
}
va_end(argsList);
[alertView show];
}
常用的類方法以及對象方法也進(jìn)行了淺層封裝
+ (instancetype)alertView {
return [[[self class] alloc] init];
}
#pragma mark - public method
#pragma mark -- class method
/**
* 類方法 提示框(只顯示消息)
*
* @param message 消息
*/
+ (void)showMessage:(NSString *)message {
[[[self class] alertView] showMessage:message];
}
/**
* 類方法 提示框(只顯示標(biāo)題和消息)
*
* @param title 標(biāo)題
* @param message 消息
*/
+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message {
[[[self class] alertView] showAlertViewWithTitle:title message:message];
}
/**
* 類方法 提示框(只顯示標(biāo)題和消息)
*
* @param title 標(biāo)題
* @param message 消息
* @param dismissCallBack 確定回調(diào)
*/
+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message dismissCallBack:(CancelCallBack)dismissCallBack {
[[[self class] alertView] showAlertViewWithTitle:title message:message dismissCallBack:dismissCallBack];
}
#pragma mark -- object method
/**
* 提示框(只顯示消息)
*
* @param message 消息
*/
- (void)showMessage:(NSString *)message {
[self showAlertViewWithTitle:nil message:message];
}
/**
* 提示框(只顯示標(biāo)題和消息)
*
* @param title 標(biāo)題
* @param message 消息
*/
- (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message {
[self showAlertViewWithTitle:title message:message cancelButtonTitle:@"確定" cancelCallBack:nil otherCallBack:nil otherButtonTitles:nil];
}
/**
* 提示框(只顯示標(biāo)題和消息)
*
* @param title 標(biāo)題
* @param message 消息
* @param dismissCallBack 確定回調(diào)
*/
- (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message dismissCallBack:(CancelCallBack)dismissCallBack {
[self showAlertViewWithTitle:title message:message cancelButtonTitle:@"確定" cancelCallBack:dismissCallBack otherCallBack:nil otherButtonTitles:nil, nil];
}
至此,我們的目標(biāo)也就達(dá)成了锁蠕。我們可以用宏定義簡單的調(diào)用系統(tǒng)提示框了
#define XXALERT(msg) [ALAlertView showMessage:msg];
注意事項:
- 若需要界面顯示時夷野,就出現(xiàn)彈窗,建議在ViewDidAppear中調(diào)用荣倾。
- 設(shè)備是iOS9及之后版本的悯搔,同時彈出多個窗口時只會顯示第一個,建議在cancelCallBack中多次彈出舌仍。
- 多個按鈕的入?yún)⒘斜?strong>otherButtonTitles的順序與otherCallBack中的返回的下標(biāo)是一一對應(yīng)的妒貌,從0開始。
Demo地址:
https://github.com/alanshen0118/ALAlertView.git
若有問題铸豁,小編會及時更正灌曙,請大家多多指教。
若覺得對您有用节芥,請點(diǎn)下方喜歡在刺,多謝支持~
Thanks~