LGAlertView三大回调机制详解:Blocks、Delegate与Notification如何选
LGAlertView三大回调机制详解:Blocks、Delegate与Notification如何选
LGAlertView 是一款高度可定制的 iOS 弹窗库(替代 UIAlertController、UIAlertView 与 UIActionSheet),它提供 Blocks、Delegate 与 Notification 三套可互相替换的回调机制。本文将结合项目源码,讲清三者的触发时机与适用场景,帮助你在弹窗按钮点击、生命周期事件等场景中快速做出正确选择。
上图来自 Demo 工程,LGAlertView 支持把任意 UIView 嵌入弹窗,这张雪山图正是模糊弹窗(Blurred Alert)示例中加载的自定义视图素材。
为什么需要三种回调机制?
系统原生 UIAlertController 只能靠闭包回调处理按钮点击,想要监听"弹窗即将显示""动画开始"等细节事件时无计可施。LGAlertView 把弹窗全生命周期的事件统一成 13 个回调点,并为每个事件同时提供三种通道:
| 事件分组 | 对应事件 | 触发时机 |
|---|---|---|
| 生命周期 | willShow / didShow |
显示动画开始前 / 显示动画结束后 |
| 生命周期 | willDismiss / didDismiss |
消失动画开始前 / 消失动画结束后 |
| 按钮点击 | action / cancel / destructive |
点击普通按钮 / 取消按钮 / 危险按钮,立即触发 |
| 点击后消失 | didDismissAfterAction / ...Cancel / ...Destructive |
点击后弹窗消失完成时触发 |
| 自定义动画 | showAnimations / dismissAnimations |
动画执行时,方便插入自己的动画 |
在源码 LGAlertView.m 中可以看到,每次按钮点击都会按顺序同时触发三种回调:先执行 Block,再调用 Delegate 方法,最后发出 Notification。也就是说三者可以共存、互不排斥,选择哪种纯粹看你的场景。
Blocks 回调:本地一次性场景的默认选择
Blocks 是最直接的方式。创建弹窗时直接赋值,用完即走,无需任何协议:
LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"提示"
message:@"确定要删除这条记录吗?"
style:LGAlertViewStyleAlert
buttonTitles:nil
cancelButtonTitle:@"取消"
destructiveButtonTitle:@"删除"];
__weak typeof(self) wself = self;
alertView.actionHandler = ^(LGAlertView *v, NSUInteger index, NSString *title) { /* 普通按钮 */ };
alertView.cancelHandler = ^(LGAlertView *v) { /* 取消 */ };
alertView.destructiveHandler = ^(LGAlertView *v) { /* 危险操作 */ };
[alertView showAnimated:YES completionHandler:nil];
所有 Handler 属性集中在 LGAlertView.h,包括生命周期(willShowHandler、didDismissHandler 等)和动画扩展(showAnimationsBlock)。
⚠️ 新手必踩的坑:Block 强持有
self会造成循环引用。务必使用__weak typeof(self)(Swift 中用[unowned self]/[weak self]),这一点在头文件注释里也被反复强调。
适合场景:回调逻辑就在当前页面、只服务于这一次弹窗——90% 的日常需求用 Blocks 就够了。
Delegate 回调:一个页面多个弹窗的集中管理
当一个页面会弹出多种弹窗、或者你不想把逻辑散落在每个创建点时,Delegate 更合适。让 ViewController 遵循 LGAlertViewDelegate 协议(定义于 LGAlertView.h),即可统一接管所有事件。官方 Demo 中的写法见 TableViewControllerAlert.m:
- (void)alertView:(LGAlertView *)alertView clickedButtonAtIndex:(NSUInteger)index title:(NSString *)title {
NSLog(@"action {title: %@, index: %lu}", title, (long unsigned)index);
}
- (void)alertViewCancelled:(LGAlertView *)alertView {
NSLog(@"cancel");
}
Delegate 有两个天然优势:
- 不会造成内存泄漏:
delegate属性声明为weak(见 LGAlertView.h),且弹窗消失完成后会自动置空; - 天然区分来源:所有方法都带着
alertView参数,多个弹窗共存时按实例分发逻辑,清晰可控。
创建时通过 initWithTitle:...delegate: 系列方法传入即可,例如:
[[[LGAlertView alloc] initWithTitle:@"Title" message:@"Message"
style:LGAlertViewStyleAlert
buttonTitles:@[@"Button 1", @"Button 2"]
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Destructive"
delegate:self] showAnimated:YES completionHandler:nil];
Notification 回调:跨模块解耦的全局广播
如果弹窗由某个组件触发,而点击结果需要被其他模块感知(比如全局日志、埋点、状态中心),Notification 是解耦度最高的选择。LGAlertView 对外暴露了 12 个通知常量,定义在 LGAlertView.m,声明在 LGAlertView.h:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onAction:)
name:LGAlertViewActionNotification
object:nil];
- (void)onAction:(NSNotification *)note {
NSString *title = note.userInfo[@"title"]; // 按钮标题
NSUInteger index = note.userInfo[@"index"].unsignedIntegerValue; // 按钮下标
}
两个实用细节:
- 通知的
object就是发出事件的LGAlertView实例,注册时传入具体实例即可精准过滤,避免"误听"别的弹窗; showAnimations/dismissAnimations两个通知的userInfo里带有duration(动画时长),方便你在同一节拍里追加自定义动画。
适合场景:弹窗的"发起方"和"结果处理方"不在同一个类,甚至不在同一个模块——监听方零耦合,随时可挂可拆。
三大回调机制怎么选:一张速查表
| 维度 | Blocks | Delegate | Notification |
|---|---|---|---|
| 耦合程度 | 强(跟随局部变量) | 弱(协议约束) | 零(全局广播) |
| 记忆点 | 需 __weak self 防循环引用 |
weak 属性,无泄漏风险 |
记得移除 observer |
| 多弹窗区分 | 各自独立,天然隔离 | 靠 alertView 参数分发 |
靠 object 过滤 |
| 适合场景 | 本地单次弹窗(默认首选) | 同页面多弹窗集中处理 | 跨模块、全局事件监听 |
| 能拿到按钮 index/title | ✅ | ✅ | ✅(userInfo) |
一句话总结:就近原则优先用 Blocks;一个页面多个弹窗选 Delegate;需要跨模块解耦用 Notification;三者可以同时生效,互不冲突。
新手常见问题与避坑指南
1. 为什么 didDismissAfterAction 没有触发?
这些"消失后"回调依赖 dismissOnAction(默认 YES):点击按钮 → 弹窗消失 → 消失完成时才触发。如果你把 dismissOnAction 设为 NO,弹窗会保持显示,这组回调也就不会在点击时触发。
2. 点击后弹窗还开着,我能拿到用户输入吗?
可以。例如输入登录框的示例(TableViewControllerAlert.m)中,Demo 持有 securityAlertView 引用,在回调里通过 textFieldsArray 读取输入内容后再手动 dismiss。
3. 回调里还能修改弹窗外观吗?
不能。所有属性只能在 show 之前修改,显示后修改无效——这也是 willShow 与 didShow 两个回调的意义所在:前者还能"赶在动画前"介入,后者则只适合读取状态。
4. 自定义动画怎么写?
通过 showAnimationsBlock / dismissAnimationsBlock(或对应 Delegate 方法、LGAlertViewShowAnimationsNotification 通知)拿到 duration,在动画窗口期执行你自己的 transform 或透明度变化。
写在最后
LGAlertView 用"Block + Delegate + Notification 三通道并行触发"的设计,把 iOS 弹窗的事件处理做到了最大化灵活:本地逻辑用 Blocks 三行搞定,页面级事件交给 Delegate,跨模块广播交给 Notification。配合 README.md 中 "Handle actions" 章节和 Demo 工程里的各场景页面(TableViewControllerAlert、TableViewControllerActionSheet 等),基本可以覆盖你项目中所有弹窗回调需求。
