84 lines
3.5 KiB
Objective-C
84 lines
3.5 KiB
Objective-C
//
|
||
// 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;
|
||
|
||
/// 自定义“确定/取消”按钮颜色(为 nil 时使用系统默认)
|
||
+ (void)confirmTitle:(nullable NSString *)title
|
||
message:(nullable NSString *)message
|
||
ok:(nullable NSString *)okTitle
|
||
cancel:(nullable NSString *)cancelTitle
|
||
okColor:(nullable UIColor *)okColor
|
||
cancelColor:(nullable UIColor *)cancelColor
|
||
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
|