UIAlertPlus 兼容iOS8以上和7以下

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

#import <UIKit/UIKit.h>
@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];
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,195评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,149评论 25 709
  • 用户体系一————登录注册认证 cookie 和 session cookie 是存在浏览器中的一段数据,是键值对...
    yipliksun阅读 3,427评论 0 1
  • 你相信吗? 这一生遇见你, 是上辈子我欠你的, 爱你并不容易, 还需要很多勇气。 也许轮回里, 早已注定, 今生就...
    RY周周阅读 1,280评论 0 2
  • 这本书看完很久了,久到我都快忘记曾经看过它,这是一本简单的小说,再简单不过的故事情节。在我看来,也不过是讲...
    梧桐119阅读 4,483评论 0 51