#iOS#HeminWon

弹框

2016-06-12  本文已影响148人  南京杨小兵

自从iOS9出来之后,需要使用UIAlertController来弹出弹框,不在提倡使用UIAlertView了,UIAlertController
, 这个类怎么说好呢,确实方便我们管理弹框,但是这个类必须在UIViewController
上面presentViewController弹出,没UIAlertView使用方便。

那么出现这个问题了,就得想有没有什么好的方式去解决,让UIAlertController使用起来跟UIAlertView
一样的方便,第一个问题,用继承还是类别,现在的目的是所有类都能直接调用,尤其是没有UI的类,所以用类别,

第二个问题,类别是哪个类,要想所有的类都能使用,方便简单的调用,那就得所有类的父类,NSObject

第三个问题,iOS系统版本,既然是好用,那就得适配的版本要尽量多,

所以,

、、、

#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

、、、

上一篇 下一篇

猜你喜欢

热点阅读