{"id":12218,"date":"2023-12-13T12:00:00","date_gmt":"2023-12-13T04:00:00","guid":{"rendered":"https:\/\/www.zhidianwl.net\/zhidianwl\/?p=12218"},"modified":"2023-12-13T12:00:00","modified_gmt":"2023-12-13T04:00:00","slug":"ios-uialertviewcontroller%e5%b0%81%e8%a3%85%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.net\/zhidianwl\/2023\/12\/13\/ios-uialertviewcontroller%e5%b0%81%e8%a3%85%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d%ef%bc%9f\/","title":{"rendered":"ios uialertviewcontroller\u5c01\u88c5\u662f\u4ec0\u4e48\u610f\u601d\uff1f"},"content":{"rendered":"
UIAlertViewController\u662fiOS\u5f00\u53d1\u4e2d\u5e38\u7528\u7684\u4e00\u4e2a\u5f39\u7a97\u63a7\u4ef6\uff0c\u53ef\u4ee5\u7528\u6765\u663e\u793a\u63d0\u793a\u4fe1\u606f\u3001\u8b66\u544a\u4fe1\u606f\u3001\u786e\u8ba4\u4fe1\u606f\u7b49\u7b49\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u591a\u6b21\u4f7f\u7528UIAlertViewController\uff0c\u4e3a\u4e86\u65b9\u4fbf\u4ee3\u7801\u590d\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06UIAlertViewController\u8fdb\u884c\u5c01\u88c5\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u4f7f\u7528\u65f6\u76f4\u63a5\u8c03\u7528\u3002<\/p>\n
UIAlertViewController\u7684\u5c01\u88c5\u539f\u7406\u4e3b\u8981\u662f\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81eaUIAlertViewController\u7684\u7c7b\uff0c\u5e76\u5728\u8be5\u7c7b\u4e2d\u7f16\u5199\u9700\u8981\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u5728\u9700\u8981\u4f7f\u7528UIAlertViewController\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u8981\u521b\u5efa\u8be5\u7c7b\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\u8c03\u7528\u5176\u4e2d\u7684\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n
\u4e0b\u9762\uff0c\u6211\u4eec\u6765\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u8fdb\u884cUIAlertViewController\u7684\u5c01\u88c5\u3002<\/p>\n
1. \u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81eaUIAlertViewController\u7684\u7c7b<\/p>\n
\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81eaUIAlertViewController\u7684\u7c7b\uff0c\u547d\u540d\u4e3aCustomAlertViewController\u3002\u5728\u8be5\u7c7b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u9700\u8981\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n
“`<\/p>\n
@interface CustomAlertViewController : UIAlertController<\/p>\n
@property (nonatomic, copy) void(^confirmBlock)(void);<\/p>\n
@property (nonatomic, copy) void(^cancelBlock)(void);<\/p>\n
– (instancetype)initWithTitle:(NSString *)title message:(NSString *)message confirmTitle:(NSString *)confirmTitle cancelTitle:(NSString *)cancelTitle;<\/p>\n
@end<\/p>\n
“`<\/p>\n
\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e09\u4e2a\u5c5e\u6027\u548c\u4e00\u4e2a\u521d\u59cb\u5316\u65b9\u6cd5\u3002\u5176\u4e2d\uff0cconfirmBlock\u548ccancelBlock\u662f\u4e24\u4e2a\u56de\u8c03\u5757\uff0c\u7528\u6765\u5904\u7406\u7528\u6237\u70b9\u51fb\u786e\u8ba4\u548c\u53d6\u6d88\u6309\u94ae\u7684\u64cd\u4f5c\u3002initWithTitle: message: confirmTitle: cancelTitle:\u65b9\u6cd5\u7528\u6765\u521d\u59cb\u5316UIAlertViewController\u7684\u6807\u9898\u3001\u6d88\u606f\u3001\u786e\u8ba4\u6309\u94ae\u548c\u53d6\u6d88\u6309\u94ae\u7684\u6807\u9898\u3002<\/p>\n
2. \u5b9e\u73b0\u521d\u59cb\u5316\u65b9\u6cd5<\/p>\n
\u5728CustomAlertViewController.m\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5b9e\u73b0initWithTitle: message: confirmTitle: cancelTitle:\u65b9\u6cd5\u3002\u5728\u8be5\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7236\u7c7b\u7684initWithTitle: message: preferredStyle:\u65b9\u6cd5\u6765\u521d\u59cb\u5316UIAlertViewController\uff0c\u5e76\u8bbe\u7f6e\u786e\u8ba4\u548c\u53d6\u6d88\u6309\u94ae\u7684\u4e8b\u4ef6\u3002<\/p>\n
“`<\/p>\n
– (instancetype)initWithTitle:(NSString *)title message:(NSString *)message confirmTitle:(NSString *)confirmTitle cancelTitle:(NSString *)cancelTitle {<\/p>\n
self = [super initWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];<\/p>\n
if (self) {<\/p>\n
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:confirmTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {<\/p>\n
if (self.confirmBlock) {<\/p>\n
self.confirmBlock();<\/p>\n
}<\/p>\n
}];<\/p>\n
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {<\/p>\n
if (self.cancelBlock) {<\/p>\n
self.cancelBlock();<\/p>\n
}<\/p>\n
}];<\/p>\n
[self addAction:confirmAction];<\/p>\n
[self addAction:cancelAction];<\/p>\n
}<\/p>\n
return self;<\/p>\n
}<\/p>\n
“`<\/p>\n
\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2aUIAlertAction\u5bf9\u8c61\uff0c\u5206\u522b\u7528\u6765\u8868\u793a\u786e\u8ba4\u548c\u53d6\u6d88\u6309\u94ae\uff0c\u5e76\u8bbe\u7f6e\u5b83\u4eec\u7684\u4e8b\u4ef6\u3002\u7136\u540e\uff0c\u6211\u4eec\u8c03\u7528\u4e86addAction:\u65b9\u6cd5\uff0c\u5c06\u4e24\u4e2aUIAlertAction\u5bf9\u8c61\u6dfb\u52a0\u5230UIAlertViewController\u4e2d\u3002<\/p>\n
3. \u5b9e\u73b0\u56de\u8c03\u5757<\/p>\n
\u5728CustomAlertViewController\u7c7b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u56de\u8c03\u5757confirmBlock\u548ccancelBlock\uff0c\u7528\u6765\u5904\u7406\u7528\u6237\u70b9\u51fb\u786e\u8ba4\u548c\u53d6\u6d88\u6309\u94ae\u7684\u64cd\u4f5c\u3002\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u8fd9\u4e24\u4e2a\u56de\u8c03\u5757\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n
“`<\/p>\n
– (void)showWithConfirmBlock:(void (^)(void))confirmBlock cancelBlock:(void (^)(void))cancelBlock {<\/p>\n
self.confirmBlock = confirmBlock;<\/p>\n
self.cancelBlock = cancelBlock;<\/p>\n
UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;<\/p>\n