This commit is contained in:
2025-11-10 15:38:30 +08:00
parent 97316c7989
commit 1cdc17b710
18 changed files with 337 additions and 20 deletions

View File

@@ -0,0 +1,75 @@
//
// KBAlert.h
// keyBoard
//
// 系统 UIAlertController 的轻量封装:更易用、更健壮。
// 特性:
// - 任意线程调用;内部切回主线程
// - 自动找到可展示的 VC使用顶层 presented VC
// - Alert 队列:避免“正在展示时再次 present 失败”的崩溃/警告
// - iPad ActionSheet 自动设置 popover 锚点(无需关心 sourceView
// - 常见场景一行调用:提示/确认/输入框/操作表
// - 可选指定展示的 VC如在 Extension 内)
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef void (^KBAlertBoolHandler)(BOOL ok);
typedef void (^KBAlertIndexHandler)(NSInteger index);
typedef void (^KBAlertTextHandler)(NSString * _Nullable text, BOOL ok);
@interface KBAlert : NSObject
/// 指定一个缺省的“用于 present 的 VC”。
/// - 适用App Extension 或自管理容器场景App 内一般无需设置。
/// - 注意:弱引用;随时可被释放。
+ (void)setDefaultPresenter:(nullable __kindof UIViewController *)presenter;
#pragma mark - 简单提示(单按钮)
/// 标准 OK 提示按钮文案默认为“好”或“OK”。
+ (void)showTitle:(nullable NSString *)title message:(nullable NSString *)message;
/// 自定义按钮文案与回调。
+ (void)showTitle:(nullable NSString *)title
message:(nullable NSString *)message
button:(nullable NSString *)button
completion:(dispatch_block_t)completion;
#pragma mark - 确认对话框(双按钮)
/// 默认“取消/确定”按钮,回调 ok=YES 表示点击了确定。
+ (void)confirmTitle:(nullable NSString *)title
message:(nullable NSString *)message
completion:(KBAlertBoolHandler)completion;
/// 自定义按钮文案。
+ (void)confirmTitle:(nullable NSString *)title
message:(nullable NSString *)message
ok:(nullable NSString *)okTitle
cancel:(nullable NSString *)cancelTitle
completion:(KBAlertBoolHandler)completion;
#pragma mark - 输入框(单行)
/// 带单个输入框ok=YES 时返回输入内容。
+ (void)promptTitle:(nullable NSString *)title
message:(nullable NSString *)message
placeholder:(nullable NSString *)placeholder
ok:(nullable NSString *)okTitle
cancel:(nullable NSString *)cancelTitle
keyboardType:(UIKeyboardType)type
configuration:(void (^ _Nullable)(UITextField *tf))config
completion:(KBAlertTextHandler)completion;
#pragma mark - 操作表ActionSheet
/// 操作表index 为点击项序号,按 actions 顺序从 0 开始;取消返回 -1。
+ (void)actionSheetTitle:(nullable NSString *)title
message:(nullable NSString *)message
actions:(NSArray<NSString *> *)actions
cancel:(nullable NSString *)cancelTitle
destructiveIndex:(NSInteger)destructiveIndex // 传入 -1 表示无
fromView:(nullable UIView *)anchorView // iPad 可指定锚点;传 nil 自动居中
completion:(KBAlertIndexHandler)completion;
@end
NS_ASSUME_NONNULL_END