UIAlerController 优化

来源:互联网 发布:网络会计若干问题探讨 编辑:程序博客网 时间:2024/06/05 16:57

目的就是缩减代码量,将重复代码放在一个类中。


DYAlertController类,如下

.h:

#import <UIKit/UIKit.h>@interface DYAlertController : UIAlertController+(instancetype)DYAlertDefaultMessage:(NSString*)defaultMessage alertTitle:(NSString*)title alertMessage:(NSString*)message perferredStyle:(UIAlertControllerStyle)perferredStyle;-(void)show :(UIViewController*)currVC;@end




.m

#import "DYAlertController.h"@interface DYAlertController ()@end@implementation DYAlertController+(instancetype)DYAlertDefaultMessage:(NSString *)defaultMessage alertTitle:(NSString *)title alertMessage:(NSString *)message perferredStyle:(UIAlertControllerStyle)perferredStyle{    DYAlertController* dyAlertController = [DYAlertController alertControllerWithTitle:title message:message preferredStyle:perferredStyle];        UIAlertAction* defaultAlertAction = [UIAlertAction actionWithTitle:defaultMessage style:UIAlertActionStyleCancel handler:nil];    [dyAlertController addAction:defaultAlertAction];        return dyAlertController;}-(void)show :(UIViewController*)currVC{    if(![currVC isEqual:NULL])        [currVC presentViewController:self animated:YES completion:nil];}@end
在viewcontroller使用中:
 DYAlertController* alrt = [DYAlertController DYAlertDefaultMessage:@"cancel" alertTitle:@"title" alertMessage:@"message" perferredStyle:UIAlertControllerStyleAlert];    [alrt addAction:[UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"执行自定义操作,去设置...");    }]];    [alrt show:self];