UIAlerController 优化

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


DYAlertController类,如下

.h:

#import 

@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];


你可能感兴趣的:(综合,应用编程)