{"id":20468829,"url":"https://github.com/xifenglang/jkalertmanager","last_synced_at":"2025-04-13T10:26:52.262Z","repository":{"id":56916018,"uuid":"65375600","full_name":"XiFengLang/JKAlertManager","owner":"XiFengLang","description":"【iOS】深度封装UIAlertController,良好兼容UIAlertControllerStyleAlert和UIAlertControllerStyleActionSheet","archived":false,"fork":false,"pushed_at":"2018-11-30T02:07:00.000Z","size":2201,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T19:19:02.052Z","etag":null,"topics":["alertcontroller","uialertcontroller","uialertview"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/XiFengLang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-10T11:02:28.000Z","updated_at":"2020-12-24T12:01:34.000Z","dependencies_parsed_at":"2022-08-20T21:20:23.160Z","dependency_job_id":null,"html_url":"https://github.com/XiFengLang/JKAlertManager","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiFengLang%2FJKAlertManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiFengLang%2FJKAlertManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiFengLang%2FJKAlertManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiFengLang%2FJKAlertManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/XiFengLang","download_url":"https://codeload.github.com/XiFengLang/JKAlertManager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248697373,"owners_count":21147318,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alertcontroller","uialertcontroller","uialertview"],"created_at":"2024-11-15T14:06:58.106Z","updated_at":"2025-04-13T10:26:52.228Z","avatar_url":"https://github.com/XiFengLang.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"![version](https://img.shields.io/badge/Version-1.1.1-blue.svg) ![platform](https://img.shields.io/badge/platform-iOS-ligtgrey.svg)  ![ios](https://img.shields.io/badge/Requirements-iOS8%2B-green.svg)\n\nJKAlertManager\n===\n\nJKAlertManager是基于UIAlertController封装的管理类，将UIAlertController分散的ActionBlcok集中到一个Blcok中，并且实现主动释放的Block，内存管理更安全，代码精简高效。\n\n## CocoaPods \n\n```C\nplatform :ios, '8.0'\n\nsource 'https://github.com/CocoaPods/Specs.git'\n\npod 'JKAlertManager', '~\u003e 1.2.1'\n```\n\n\n## 目录\n\n* [JKAlertManager简介](#JKAlertManager)\n* [JKAlertManager迭代记录](#Updation)\n* [JKAlertManager用法](#JKAlertManagerUsage)\n  * [UIAlertControllerStyleAlert样式](#UIAlertControllerStyleAlert)\n  * [UIAlertControllerStyleAlert类型带TextField](#TextField)\n  * [UIAlertControllerStyleActionSheet样式](#UIAlertControllerStyleActionSheet)\n* [JKAlertManager部分代码用意介绍](#JKAlertManagerCode)\n* [快速替换掉工程中所有的UIAlertView](#UIAlertView)\n* [封装UIAlertView、UIActionSheet的Block分类,替换代理方法](#UIAlertViewBlock)\n\n\n\n## \u003ca id=\"JKAlertManager\"\u003e\u003c/a\u003e JKAlertManager简介\n\n* 基于UIAlertController封装，兼容UIAlertControllerStyleAlert和UIAlertControllerStyleActionSheet\n* UIAlertControllerStyleActionSheet类型兼容了iPad中的PopoverController\n* 主动释放Block，内存管理更安全，有效规避Block循环引用\n* 支持监听多个UITextField的文字内容变化，并通过Block回调监听结果\n\n\n\u003cimg src=\"http://wx3.sinaimg.cn/mw690/c56eaed1gy1fetajy78s4j20af0ijdi3.jpg\" width=\"230\" height=\"400\"\u003e\u003cimg src=\"http://wx4.sinaimg.cn/mw690/c56eaed1gy1fetajyiufyj20af0ijjtg.jpg\" width=\"230\" height=\"400\"\u003e\u003cimg src=\"http://wx4.sinaimg.cn/mw690/c56eaed1gy1fetajyuefij20af0ijjtb.jpg\" width=\"230\" height=\"400\"\u003e\n\n## \u003ca id=\"Updation\"\u003e\u003c/a\u003e JKAlertManager迭代记录\n\n\u003e * 1.0.4，增加JKAlertView类用于全局替换UIAlertView。\n\u003e * 1.1.0，JKAlertManager继承NSObject, 不再继承UIView。\n\n## \u003ca id=\"JKAlertManagerUsage\"\u003e\u003c/a\u003e JKAlertManager用法 \n\n### \u003ca id=\"UIAlertControllerStyleAlert\"\u003e\u003c/a\u003e UIAlertControllerStyleAlert样式\n\n```Object-C\n\t JKAlertManager * manager = [[JKAlertManager alloc] initWithPreferredStyle:UIAlertControllerStyleAlert title:\u003c#title#\u003e message:\u003c#nil#\u003e];\n \t [manager configueCancelTitle:\u003c#@\"取消\"#\u003e destructiveIndex:JKAlertDestructiveIndexNone otherTitles:\u003c#array#\u003e];\n\t [manager showAlertFromController:self actionBlock:^(JKAlertManager *tempAlertManager, NSInteger actionIndex, NSString *actionTitle) {\n\t \tif (actionIndex != tempAlertManager.cancelIndex) {\n \t\t\t\u003c#doSomething#\u003e\n \t\t}\n\t }];\n```\n\n### \u003ca id=\"TextField\"\u003e\u003c/a\u003e UIAlertControllerStyleAlert类型带TextField\n\n```Object-C\n\tJKAlertManager * manager = [[JKAlertManager alloc]initWithPreferredStyle:UIAlertControllerStyleAlert title:self.dataArray[indexPath.row] message:self.dataArray[indexPath.row] ];\n\t[manager configueCancelTitle:@\"取消\" destructiveIndex:1 otherTitles:@\"其他按钮1\",@\"Destructive按钮\",@\"其他按钮2\", nil];\n\t[manager addTextFieldWithPlaceholder:@\"请输入账号\" secureTextEntry:NO ConfigurationHandler:nil textFieldTextChanged:^(UITextField *textField) {\n         NSLog(@\"1   %@\",textField.text);\n\t}];\n\t[manager addTextFieldWithPlaceholder:@\"请输入密码\" secureTextEntry:YES ConfigurationHandler:^(UITextField *textField) {\n        textField.clearsOnBeginEditing = YES;\n\t} textFieldTextChanged:^(UITextField *textField) {\n        NSLog(@\"2   %@\",textField.text);\n\t}];\n    [manager showAlertFromController:self actionBlock:^(JKAlertManager *tempAlertManager, NSInteger actionIndex, NSString *actionTitle) {\n         UITextField * userNameTextField = tempAlertManager.textFields[0];\n         UITextField * passwordTextField = tempAlertManager.textFields[1];\n    }];\n\n```\n\n### \u003ca id=\"UIAlertControllerStyleActionSheet\"\u003e\u003c/a\u003e UIAlertControllerStyleActionSheet样式\n\n如果App支持iPad，需调用`configuePopoverControllerForActionSheetStyleWithSourceView：`适配iPad。\n\n```Object-C\n\tJKAlertManager * manager = [[JKAlertManager alloc]initWithPreferredStyle:UIAlertControllerStyleActionSheet title:@\"title\" message:@\"massage\" ];\n\t[manager configueCancelTitle:nil destructiveIndex:0 otherTitles:@\"Destructive按钮\",@\"其他按钮1\",@\"其他按钮2\", nil];\n\t[manager configuePopoverControllerForActionSheetStyleWithSourceView:cell sourceRect:cell.bounds popoverArrowDirection:UIPopoverArrowDirectionAny];\n\t[manager showAlertFromController:self actionBlock:^(JKAlertManager *tempAlertManager, NSInteger actionIndex, NSString *actionTitle) {\n       self...\n\t}];\n\n```\n\n## \u003ca id=\"JKAlertManagerCode\"\u003e\u003c/a\u003e JKAlertManager部分代码用意介绍\n\n\n* 之前封装的难点主要在于拆分`(NSString *)otherTitle, ...NS_REQUIRES_NIL_TERMINATION`，将otherTitle转换成数组，用法见Demo或者[NS_REQUIRES_NIL_TERMINATION](http://www.jianshu.com/p/f61ff5e72b72)。但后来发现并没啥优越性，可以直接传NSArray。\n解除Block循环引用的思路参考了 `AFNetworking`，即调用了Block后，再将Block = nil置空，即可解除循环引用，这种思路只适合调用一次的Block，多次调用的Block还是需要进行self强弱转换。\n\n\n* `1.1.0`版中，JKAlertManager 改成继承 NSObject,被内部`__JKAlertManagerPrivateHolder`类私有View强引用，JKAlertManager则会弱引用这个私有View。详情请看下面的代码:\n\n```Object-C\n@interface __JKAlertManagerPrivateHolder : UIView\n@property (nonatomic, strong) JKAlertManager * alertManager;\n@end\n@implementation __JKAlertManagerPrivateHolder\n@end\n\n\n@interface JKAlertManager ()\n@property (nonatomic, weak) __JKAlertManagerPrivateHolder * privateHolder;\n@end\n\n```\n\n私有View `privateHolder `在`showAlertFromController:(UIViewController *)ViewController`方法中添加到控制器ViewController.view上，间接实现控制器对JKAlertManager的强引用。`privateHolder`调用`removeFromSuperView`来解除强引用，破坏原来的引用链，从而实现自释放。\n\n```Object-C\n- (void)showAlertFromController:(UIViewController *)controller actionBlock:(JKAlertActionBlock)actionBlock{\n\n    if (self.privateHolder) {\n        [self.privateHolder removeFromSuperview];\n        self.privateHolder = nil;\n    }\n    \n    \n    /// 私有类，强引用JKAlertManager\n    __JKAlertManagerPrivateHolder * privateHolder = [[__JKAlertManagerPrivateHolder alloc] initWithFrame:CGRectZero];\n    privateHolder.backgroundColor = [UIColor clearColor];\n    privateHolder.alertManager = self;\n    self.privateHolder = privateHolder;\n    [controller.view addSubview:privateHolder];\n    \n    \n    [controller presentViewController:self.alertController animated:YES completion:nil];\n    \n    // 解除Block循环引用，释放内存\n    __weak typeof(self) weakSelf = self;\n    self.privateBlock = ^(NSInteger actionIndex, NSString * actionTitle){\n        __strong typeof(weakSelf) strongSelf = weakSelf;\n        if (actionBlock) actionBlock(strongSelf, actionIndex, actionTitle);\n        \n        [[NSNotificationCenter defaultCenter]removeObserver:strongSelf];\n        strongSelf.alertController = nil;\n        strongSelf.otherTitles = nil;\n        \n        /// 主动释放Block，解除循环引用\n        [strongSelf.privateHolder removeFromSuperview];\n        strongSelf.textFieldChangedBlockMutDict = nil;\n        strongSelf.privateBlock = nil;\n    };\n}\n\n```\n\n\n## \u003ca id=\"UIAlertView\"\u003e\u003c/a\u003e 快速替换掉工程中所有的UIAlertView，换用UIAlertController\n\n如果项目中用到的UIAlertView相当多，同时又想全部换成UIAlertController，可用JKAlertView全局替换UIAlertView，大部分的UIAlertView都能替换，同时不用改原来的逻辑代码和代理方法。现在UIAlertView使用过程中并无异常，所以替不替换都无所谓。**如果不需要全局替换UIAlertView,JKAlertView可删除掉**，详情请查看工程中`ReplaceViewController.m`文件中的代码。\n\n1. `#import \"JKAlertView.h\"`\n\n2. 同时按`Command + Option + Shift + F`组合键`，用JKAlertView全局替换UIAlertView\n\n\n\n## \u003ca id=\"UIAlertViewBlock\"\u003e\u003c/a\u003e基于Runtime+Block封装的UIAlertView和UIActionSheet分类\n\n\u003e * 一个UIAlertView或UIActionSheet对象对应一个Block，代码紧凑。\n\u003e * 实现Block主动释放，规避Block循环引用，不会出现内存泄露。\n\n如果控制器使用到多个`UIAlertView或者UIActionSheet`，那么需要在多个地方创建对象并设置`delegate和tag`，然后集中在一个代理方法中先根据tag区分对象，再区分`buttonIndex`，这样写的代码比较分散，可读性不强。封装后的`UIAlertView或者UIActionSheet`使用代码可读性更强，代码更紧凑。\n\n不建议使用strong修饰alertView或者actionSheet属性，如果非得用属性，建议使用weak。\n```Object-C\n@property (nonatomic, strong)UIAlertView * alertView;\n@property (nonatomic, strong)UIActionSheet * actionSheet;\n```\n\n```Objct-C\n    self.alertView = [[UIAlertView alloc]initWithTitle:@\"title\" message:@\"message\" delegate:nil cancelButtonTitle:@\"取消\" otherButtonTitles:@\"其他\", nil];\n    [self.alertView showAlertViewWithActionBlock:^(UIAlertView *newAlertView, NSInteger buttonIndex) {\n        self...\n    }];\n    \n    \n    self.actionSheet = [[UIActionSheet alloc]initWithTitle:@\"title\" delegate:nil cancelButtonTitle:@\"取消\" destructiveButtonTitle:@\"destructiveButtonTitle\" otherButtonTitles:@\"otherButtonTitles\", nil];\n    [self.actionSheet showActionSheetInView:self.view completion:^(UIActionSheet *originalActionSheet, NSInteger buttonIndex) {\n        self...\n    }];\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxifenglang%2Fjkalertmanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxifenglang%2Fjkalertmanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxifenglang%2Fjkalertmanager/lists"}