{"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

[rootVC prangular \u6253\u5305\u6210apk<\/a>esentViewController:self animated:YES completion:nil];<\/p>\n

}<\/p>\n

“`<\/p>\n

\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3ashowWithConfirmBlock:cancelBlock:\u7684\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7528\u6765\u663e\u793aUIAlertViewController\u5e76\u8bbe\u7f6e\u786e\u8ba4\u548c\u53d6\u6d88\u6309\u94ae\u7684\u4e8b\u4ef6\u3002\u5728\u8be5\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5c06confirmBlock\u548ccancelBlock\u8d4b\u503c\u7ed9CustomAlertViewController\u7c7b\u4e2d\u5b9a\u4e49\u7684\u4e24\u4e2a\u56de\u8c03\u5757\uff0c\u5e76\u83b7\u53d6\u5f53\u524d\u5e94\u7528\u7a0b\u5e8f\u7684\u6839\u89c6\u56fe\u63a7\u5236\u5668\uff0c\u7136\u540e\u8c03\u7528presentViewController:animated:completion:\u65b9\u6cd5\u663e\u793aUIAlertViewController\u3002<\/p>\n

4. \u4f7f\u7528\u81ea\u5b9a\u4e49\u5f39\u7a97<\/p>\n

\u5728\u5b9e\u9645\u4f7f\u7528CustomAlertViewController\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u8981\u521b\u5efa\u8be5\u7c7b\u7684\u5b9e\u4f8b\uff0c\u5e76\u8c03\u7528showWithConfirmBlock:cancelBlock:\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n

“`<\/p>\n

CustomAlertViewController *alertVC = [[CustomAlertViewController alloc] initWithTitle:@”\u63d0\u793a” message:@”\u786e\u5b9a\u8981\u9000\u51fa\u767b\u5f55\u5417\uff1f” confirmTitle:@”\u786e\u5b9a” cancelTitle:@”\u53d6\u6d88”];<\/p>\n

[alertVC showWithConfirmBlock:^{<\/p>\n

\/\/ \u5904\u7406<\/p>\n

<\/figure>\n<\/p>\n

\u786e\u8ba4\u6309\u94ae\u7684\u64cd\u4f5c<\/p>\n

} cancelBlock:^{<\/p>\n

\/\/ \u5904\u7406\u53d6\u6d88\u6309\u94ae\u7684\u64cd\u4f5c<\/p>\n

}];<\/p>\n

“`<\/p>\n

\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5b89\u5353\u8f6ciosapp<\/a>CustomAlertViewController\u7684\u5b9e\u4f8b\uff0c\u8bbe\u7f6e\u4e86\u6807\u9898\u3001\u6d88\u606f\u3001\u786e\u8ba4\u6309\u94ae\u548c\u53d6\u6d88\u6309\u94ae\u7684\u6807\u9898\uff0c\u7136\u540e\u8c03\u7528showWithConfirmBlock:cancelBlock:\u65b9\u6cd5\u663e\u793a\u8be5\u5f39\u7a97\u3002\u5728\u56de\u8c03\u5757\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u5904\u7406\u786e\u8ba4\u548c\u53d6\u6d88\u6309\u94ae\u4e8b\u4ef6\u7684\u4ee3\u7801\u3002<\/p>\n

\u603b\u7ed3<\/p>\n

\u901a\u8fc7\u5bf9UIAlertViewController\u8fdb\u884c\u5c01\u88c5\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u590d\u7528\u8be5\u63a7\u4ef6\uff0c\u5e76\u4e14\u53ef\u4ee5\u5c06\u5176\u4e0e\u5904\u7406\u4e8b\u4ef6\u7684\u4ee3\u7801\u5206\u79bb\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7ef4\u62a4\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9CustomAlertViewController\u8fdb\u884c\u6269\u5c55\uff0c\u589e\u52a0\u66f4\u591a\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"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\u7528UIAlertViewControll<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[17477,1846,41,695,219],"topic":[],"class_list":["post-12218","post","type-post","status-publish","format-standard","hentry","category-kaifaapp","tag-ios","tag-app","tag-41","tag-695","tag-219"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/12218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/comments?post=12218"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/12218\/revisions"}],"predecessor-version":[{"id":12272,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/12218\/revisions\/12272"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/media?parent=12218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/categories?post=12218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/tags?post=12218"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/topic?post=12218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}