自從iOS9出來(lái)之后充尉,需要使用UIAlertController來(lái)彈出彈框玩敏,不在提倡使用UIAlertView了,UIAlertController
琅捏, 這個(gè)類(lèi)怎么說(shuō)好呢,確實(shí)方便我們管理彈框递雀,但是這個(gè)類(lèi)必須在UIViewController
上面presentViewController彈出柄延,沒(méi)UIAlertView使用方便。
那么出現(xiàn)這個(gè)問(wèn)題了缀程,就得想有沒(méi)有什么好的方式去解決搜吧,讓UIAlertController使用起來(lái)跟UIAlertView
一樣的方便,第一個(gè)問(wèn)題杨凑,用繼承還是類(lèi)別滤奈,現(xiàn)在的目的是所有類(lèi)都能直接調(diào)用,尤其是沒(méi)有UI的類(lèi)撩满,所以用類(lèi)別蜒程,
第二個(gè)問(wèn)題绅你,類(lèi)別是哪個(gè)類(lèi),要想所有的類(lèi)都能使用昭躺,方便簡(jiǎn)單的調(diào)用忌锯,那就得所有類(lèi)的父類(lèi),NSObject
第三個(gè)問(wèn)題领炫,iOS系統(tǒng)版本偶垮,既然是好用,那就得適配的版本要盡量多帝洪,
所以似舵,
、葱峡、砚哗、
#import#import#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0#define IPHONE_OS_VERSION_MIN_IPHONE_8_0? ? //less than 8.0#else#define IPHONE_OS_VERSION_MAX_IPHONE_8_0? ? //greater than or equal to 8.0#endif#define NO_USE -1000typedef void(^click)(NSInteger index);typedef void(^configuration)(UITextField *field, NSInteger index);typedef void(^clickHaveField)(NSArray*fields, NSInteger index);@interface NSObject (LZAlertView)#ifdef IPHONE_OS_VERSION_MIN_IPHONE_8_0#endif/*! *? use alert * *? @param title? ? title *? @param message? message *? @param others? other button title *? @param animated animated *? @param click? ? button action */- (void)YXBAlertWithTitle:(NSString *)title? ? ? ? ? ? ? ? message:(NSString *)message? ? ? ? ? ? ? andOthers:(NSArray*)others? ? ? ? ? ? ? ? animated:(BOOL)animated? ? ? ? ? ? ? ? ? action:(click)click;/*! *? use action sheet * *? @param title? ? ? ? ? ? title *? @param message? ? ? ? ? message just system verson max or equal 8.0. *? @param destructive? ? ? button title is red color *? @param destructiveAction destructive action *? @param others? ? ? ? ? ? other button *? @param animated? ? ? ? ? animated *? @param click? ? ? ? ? ? other button action */- (void)YXBActionSheetWithTitle:(NSString *)title? ? ? ? ? ? ? ? ? ? ? message:(NSString *)message? ? ? ? ? ? ? ? ? destructive:(NSString *)destructive? ? ? ? ? ? destructiveAction:(click )destructiveAction? ? ? ? ? ? ? ? ? ? andOthers:(NSArray*)others? ? ? ? ? ? ? ? ? ? ? animated:(BOOL )animated? ? ? ? ? ? ? ? ? ? ? ? action:(click )click;/*! *? use alert include textField * *? @param title? ? ? ? title *? @param message? ? ? message *? @param buttons? ? ? buttons *? @param number? ? ? ? number of textField *? @param configuration configuration textField property *? @param animated? ? ? animated *? @param click? ? ? ? button action */- (void)YXBAlertWithTitle:(NSString *)title? ? ? ? ? ? ? ? message:(NSString *)message? ? ? ? ? ? ? ? buttons:(NSArray*)buttons
textFieldNumber:(NSInteger )number
configuration:(configuration )configuration
animated:(BOOL )animated
action:(clickHaveField )click;
@end
、砰奕、蛛芥、
.m文件
、脆淹、、
////? NSObject+LZAlertView.m//? jglz////? Created by jglz on 16/6/12.//? Copyright ? 2016年 yxb. All rights reserved.//#import "NSObject+LZAlertView.h"#import "AppDelegate.h"#ifdef IPHONE_OS_VERSION_MIN_IPHONE_8_0static click clickIndex = nil;static clickHaveField clickIncludeFields = nil;static click clickDestructive = nil;#endifstatic NSMutableArray *fields = nil;@implementation NSObject (LZAlertView)#pragma mark - *****? alert view- (void)YXBAlertWithTitle:(NSString *)title? ? ? ? ? ? ? ? message:(NSString *)message? ? ? ? ? ? ? andOthers:(NSArray*)others? ? ? ? ? ? ? ? animated:(BOOL)animated? ? ? ? ? ? ? ? ? action:(click)click{#ifdef IPHONE_OS_VERSION_MAX_IPHONE_8_0? ? ? ? UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];? ? ? ? [others enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {? ? ? ? ? ? ? ? if (idx == 0)? ? ? ? {? ? ? ? ? ? [alertController addAction:[UIAlertAction actionWithTitle:obj style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (action)? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? click(idx);? ? ? ? ? ? ? ? }? ? ? ? ? ? }]];? ? ? ? }? ? ? ? else? ? ? ? {? ? ? ? ? ? [alertController addAction:[UIAlertAction actionWithTitle:obj style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (action)? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? click(idx);? ? ? ? ? ? ? ? }? ? ? ? ? ? }]];? ? ? ? }? ? }];//? ? NSLog(@"%@",[[self currentViewController] class]);? ? UIViewController *vc =[self currentViewController];? ? [vc presentViewController:alertController animated:YES completion:nil];#else? ? UIAlertView *alertView = nil;? ? if (others.count > 0)? ? {? ? ? ? alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:others[0] otherButtonTitles: nil];? ? }? ? else? ? {? ? ? ? alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];? ? }? ? ? ? [others enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {? ? ? ? ? ? ? ? if (idx != 0)? ? ? ? {? ? ? ? ? ? [alertView adButtonWithTitle:obj];? ? ? ? }? ? }];? ? ? ? clickIndex = click;? ? ? ? [alertView show];#endif}#pragma mark - *****? alertView delegate#ifdef IPHONE_OS_VERSION_MIN_IPHONE_8_0- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{? ? if (clickIndex)? ? {? ? ? ? clickIndex(buttonIndex);? ? }? ? else if (clickIncludeFields)? ? {? ? ? ? clickIncludeFields(fields,buttonIndex);? ? }? ? ? ? clickIndex = nil;? ? clickIncludeFields = nil;}#endif#pragma mark - *****? sheet- (void)YXBActionSheetWithTitle:(NSString *)title? ? ? ? ? ? ? ? ? ? ? message:(NSString *)message? ? ? ? ? ? ? ? ? destructive:(NSString *)destructive? ? ? ? ? ? destructiveAction:(click )destructiveAction? ? ? ? ? ? ? ? ? ? andOthers:(NSArray*)others? ? ? ? ? ? ? ? ? ? ? animated:(BOOL )animated? ? ? ? ? ? ? ? ? ? ? ? action:(click )click{#ifdef IPHONE_OS_VERSION_MAX_IPHONE_8_0? ? UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];? ? ? ? [alertController addAction:[UIAlertAction actionWithTitle:destructive style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {? ? ? ? if (action)? ? ? ? {? ? ? ? ? ? destructiveAction(NO_USE);? ? ? ? }? ? }]];? ? ? ? [others enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {? ? ? ? if (idx == 0)? ? ? ? {? ? ? ? ? ? [alertController addAction:[UIAlertAction actionWithTitle:obj style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {? ? ? ? ? ? ? ? if (action)? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? click(idx);? ? ? ? ? ? ? ? }? ? ? ? ? ? }]];? ? ? ? }? ? ? ? else? ? ? ? {? ? ? ? ? ? [alertController addAction:[UIAlertAction actionWithTitle:obj style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {? ? ? ? ? ? ? ? if (action)? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? click(idx);? ? ? ? ? ? ? ? }? ? ? ? ? ? }]];? ? ? ? }? ? }];? ? UIViewController *vc =[self currentViewController];? ? [vc presentViewController:alertController animated:animated completion:nil];#else? ? UIActionSheet *sheet = nil;? ? if (others.count > 0)? ? {? ? ? ? sheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:others[0] destructiveButtonTitle:destructive otherButtonTitles:nil];? ? }? ? else? ? {? ? ? ? sheet = [[UIActionSheet alloc] initWithTitle:title delegate:nil cancelButtonTitle:nil destructiveButtonTitle:destructive otherButtonTitles:nil];? ? }? ? ? ? [others enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {? ? ? ? if (idx != 0)? ? ? ? {? ? ? ? ? ? [sheet addButtonWithTitle:obj];? ? ? ? }? ? }];? ? clickIndex = click;? ? clickDestructive = destructiveAction;? ? [sheet showInView:self.view];#endif}#ifdef IPHONE_OS_VERSION_MIN_IPHONE_8_0- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{? ? if (buttonIndex == 0)? ? {? ? ? ? if (clickDestructive)? ? ? ? {? ? ? ? ? ? clickDestructive(NO_USE);? ? ? ? }? ? }? ? else? ? {? ? ? ? if (clickIndex)? ? ? ? {? ? ? ? ? ? clickIndex(buttonIndex - 1);? ? ? ? }? ? }}#endif#pragma mark - *****? textField- (void)YXBAlertWithTitle:(NSString *)title? ? ? ? ? ? ? ? message:(NSString *)message? ? ? ? ? ? ? ? buttons:(NSArray*)buttons
textFieldNumber:(NSInteger )number
configuration:(configuration )configuration
animated:(BOOL )animated
action:(clickHaveField )click
{
if (fields == nil)
{
fields = [NSMutableArray array];
}
else
{
[fields removeAllObjects];
}
#ifdef IPHONE_OS_VERSION_MAX_IPHONE_8_0
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
// textfield
for (NSInteger i = 0; i < number; i++)
{
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
[fields addObject:textField];
configuration(textField,i);
}];
}
// button
[buttons enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (idx == 0)
{
[alertController addAction:[UIAlertAction actionWithTitle:obj style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
if (action)
{
click(fields,idx);
}
}]];
}
else
{
[alertController addAction:[UIAlertAction actionWithTitle:obj style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (action)
{
click(fields,idx);
}
}]];
}
}];
UIViewController *vc =[self currentViewController];
[vc presentViewController:alertController animated:animated completion:nil];
#else
UIAlertView *alertView = nil;
if (buttons.count > 0)
{
alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:buttons[0] otherButtonTitles:nil];
}
else
{
alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
}
// field
if (number == 1)
{
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
}
else if (number > 2)
{
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
}
// configuration field
if (alertView.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput)
{
[fields addObject:[alertView textFieldAtIndex:0]];
[fields addObject:[alertView textFieldAtIndex:1]];
configuration([alertView textFieldAtIndex:0],0);
configuration([alertView textFieldAtIndex:1],1);
}
else if(alertView.alertViewStyle == UIAlertViewStylePlainTextInput || alertView.alertViewStyle == UIAlertViewStyleSecureTextInput)
{
[fields addObject:[alertView textFieldAtIndex:0]];
configuration([alertView textFieldAtIndex:0],0);
}
// other button
[buttons enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (idx != 0)
{
[alertView addButtonWithTitle:obj];
}
}];
clickIncludeFields = click;
[alertView show];
#endif
}
- (UIViewController *)currentViewController
{
AppDelegate *app = [UIApplication sharedApplication].delegate;
UIViewController *vc = app.window.rootViewController;
if ([vc isKindOfClass:[UITabBarController class]]) {
UITabBarController *tab = (UITabBarController *)vc;
if ([tab.selectedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)tab.selectedViewController;
return [nav.viewControllers lastObject];
} else {
return tab.selectedViewController;
}
} else if ([vc isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)vc;
return [nav.viewControllers lastObject];
} else {
return vc;
}
return vc;
}
@end
沽一、盖溺、、