UIAlertPlus 兼容iOS8以上和7以下

看到网上还一直是uialertview的天下, 决定写个兼容, 毕竟这个随时被淘汰
写的并不灵活, 但包含了文本输入, 其中的按钮事件要分开写
头文件

#import 
@interface UIAlertPlus : NSObject
+(void)showAlertWithTitle:(NSString*)title msg:(NSString*)msg delegate:(id)delegate cancelBt:(NSString*)cancel otherBt:(NSString*)other inputTip:(NSString*)inputTip cancelBlock:(void (^ __nullable)(UIAlertController* alert, UIAlertAction *action))cancelBlock okBlock:(void (^ __nullable)(UIAlertController* alert, UIAlertAction *action))okBlock;
@end

实现文件

//
//  UIAlertPlus.m
//  TestDemoConfig
//
//  Created by Zszen John on 2017/7/9.
//  Copyright © 2017年 Zstudio. All rights reserved.
//
#import "UIAlertPlus.h"
@implementation UIAlertPlus

+(void)showAlertWithTitle:(NSString*)title msg:(NSString*)msg delegate:(id)delegate cancelBt:(NSString*)cancel otherBt:(NSString*)other inputTip:(NSString*)inputTip cancelBlock:(void (^ __nullable)(UIAlertController* alert, UIAlertAction *action))cancelBlock okBlock:(void (^ __nullable)(UIAlertController* alert, UIAlertAction *action))okBlock{
    if([[[UIDevice currentDevice] systemVersion]intValue]<8){
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:delegate cancelButtonTitle:cancel otherButtonTitles:other, nil];
        if (inputTip==nil) {
            alertView.alertViewStyle = UIAlertViewStyleDefault;
        }else{
            alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
            UITextField *txtInput = [alertView textFieldAtIndex:0];//设置输入框
            txtInput.placeholder=inputTip;
            txtInput.delegate=delegate;
        }
        [alertView show];
    }else{
        UIAlertController* alertController = [UIAlertController alertControllerWithTitle:title
                                                                       message:msg
                                                                preferredStyle:UIAlertControllerStyleAlert];//
        if(other!=nil && okBlock!=nil){
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:other style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if(okBlock!=nil) okBlock(alertController,action);
            }];
            [alertController addAction:defaultAction];
        }
        if(cancel!=nil && cancelBlock!=nil){
            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                if(cancelBlock!=nil) cancelBlock(alertController,action);
            }];
            [alertController addAction:cancelAction];
        }
        if(inputTip!=nil){
            [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
                textField.placeholder = inputTip;
            }];
        }
        [delegate presentViewController:alertController animated:YES completion:nil];
    }
}
@end

运用方法:

{
//in function
[UIAlertPlus showAlertWithTitle:@"保存到剪贴板" msg:nil delegate:self cancelBt:@"取消" otherBt:@"确定" inputTip:@"输入滤镜名称" cancelBlock:nil okBlock:^(UIAlertController *alert, UIAlertAction *action) {
            UITextField* txtInput = alert.textFields.firstObject;
            [self snipToClipboard:txtInput];
        }];
}
//ios7 支持
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    //只写了点击确定按钮的事件可以根据需要来设置多个按钮的点击事件
    if (buttonIndex == alertView.firstOtherButtonIndex) {
        UITextField *txtInput = [alertView textFieldAtIndex:0];
        [self snipToClipboard:txtInput];
    }
}

你可能感兴趣的:(UIAlertPlus 兼容iOS8以上和7以下)