{"id":270971,"date":"2024-05-16T12:42:30","date_gmt":"2024-05-16T04:42:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/270971.html"},"modified":"2024-05-16T12:42:37","modified_gmt":"2024-05-16T04:42:37","slug":"ios-%e9%a1%b9%e7%9b%ae%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0-block-%e4%bc%a0%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/270971.html","title":{"rendered":"iOS \u9879\u76ee\u5982\u4f55\u5b9e\u73b0 block \u4f20\u503c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24193740\/babd6802-e571-4fb4-b538-3ebe8f9f5c24.webp\" alt=\"iOS \u9879\u76ee\u5982\u4f55\u5b9e\u73b0 block \u4f20\u503c\" \/><\/p>\n<p><p>\u5728iOS\u9879\u76ee\u4e2d\uff0c<strong>block\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u7528\u4e8e\u5b9e\u73b0\u56de\u8c03\u548c\u6570\u636e\u4f20\u9012<\/strong>\u3002\u5b9e\u73b0block\u4f20\u503c\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u542b\u5b9a\u4e49block\u5c5e\u6027\u6216\u65b9\u6cd5\u53c2\u6570\u3001\u8bbe\u7f6eblock\u5b9e\u73b0\u3001\u8c03\u7528block\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u4f7f\u7528block\u4f20\u503c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6a21\u5757\u5316\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9a\u4e49block\u7c7b\u578b<\/strong>\u662f\u5b9e\u73b0block\u4f20\u503c\u7684\u7b2c\u4e00\u6b65\u3002\u901a\u8fc7\u786e\u5b9a\u6570\u636e\u4f20\u9012\u7684\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u9002\u5f53\u7684block\uff0c\u5b83\u63a5\u6536\u9700\u8981\u7684\u53c2\u6570\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u7c7b\u578b\u3002\u901a\u5e38\u5728\u7c7b\u7684\u63a5\u53e3\u90e8\u5206\u58f0\u660eblock\u5c5e\u6027\u6216\u8005\u4f5c\u4e3a\u65b9\u6cd5\u7684\u53c2\u6570\u5f15\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001BLOCK\u5b9a\u4e49\u548c\u58f0\u660e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0block\u4f20\u503c\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u597dblock\u7684\u7c7b\u578b\u3002\u8fd9\u901a\u5e38\u53d1\u751f\u5728\u67d0\u4e2a\u7c7b\u7684\u5934\u6587\u4ef6\uff08.h\uff09\u4e2d\u3002\u5047\u8bbe\u6211\u4eec\u60f3\u4f20\u9012\u4e00\u4e2aNSString\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4e00\u4e2a\u7b80\u5355\u7684block\u5b9a\u4e49\u770b\u8d77\u6765\u53ef\u80fd\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-objc\">typedef void (^MyCompletionBlock)(NSString *result);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u7c7b\u7684\u63a5\u53e3\u6216\u5b9e\u73b0\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528<code>@property<\/code>\u58f0\u660eblock\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-objc\">@property (nonatomic, copy) MyCompletionBlock myBlock;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001BLOCK\u7684\u521b\u5efa\u548c\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u53d1\u9001\u8005\u7684\u7c7b\u4e2d\uff0c\u60a8\u53ef\u80fd\u6709\u4e00\u4e2a\u65b9\u6cd5\u6765\u914d\u7f6e\u8981\u4f20\u9012\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-objc\">- (void)configureBlock {<\/p>\n<p>    self.myBlock = ^(NSString *result) {<\/p>\n<p>        \/\/ \u5728\u8fd9\u91cc\u5904\u7406\u4f20\u56de\u7684result<\/p>\n<p>        NSLog(@&quot;Result received: %@&quot;, result);<\/p>\n<p>    };<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001BLOCK\u7684\u4f7f\u7528\u548c\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u63a5\u6536\u503c\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u4e4b\u524d\u521b\u5efa\u5e76\u914d\u7f6e\u7684block\u6765\u5b9e\u73b0\u503c\u7684\u4f20\u9012\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-objc\">- (void)executeBlockWithResult:(NSString *)result {<\/p>\n<p>    if (self.myBlock) {<\/p>\n<p>        self.myBlock(result);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001BLOCK\u5728\u754c\u9762\u95f4\u4f20\u503c<\/h3>\n<\/p>\n<p><p>Block\u5e38\u7528\u4e8eUIViewController\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u3002\u5f53\u4ece\u4e00\u4e2a\u754c\u9762\u8df3\u8f6c\u5230\u53e6\u4e00\u4e2a\u754c\u9762\u5e76\u671f\u671b\u5f97\u5230\u8fd4\u56de\u503c\u65f6\uff0c\u53ef\u4ee5\u5728\u8df3\u8f6c\u524d\u8bbe\u7f6eblock\u8fdb\u884c\u503c\u7684\u6355\u83b7\u3002<\/p>\n<\/p>\n<p><p><strong>\u5728\u6e90\u89c6\u56fe\u63a7\u5236\u5668<\/strong>\uff08source view controller\uff09\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-objc\">- (void)prepareForSegueForSegue:(UIStoryboardSegue *)segue sender:(id)sender {<\/p>\n<p>    if ([segue.identifier isEqualToString:@&quot;showDet<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>lSegue&quot;]) {<\/p>\n<p>        DetailViewController *detailVC = segue.destinationViewController;<\/p>\n<p>        detailVC.completionBlock = ^(NSString *returnValue) {<\/p>\n<p>            \/\/ \u4f7f\u7528returnValue\u66f4\u65b0UI\u6216\u6570\u636e<\/p>\n<p>            self.label.text = returnValue;<\/p>\n<p>        };<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u76ee\u6807\u89c6\u56fe\u63a7\u5236\u5668<\/strong>\uff08destination view controller\uff09\u4e2d\uff0c\u8bbe\u7f6e\u4e00\u4e2a\u5c5e\u6027\u6765\u6301\u6709\u8fd9\u4e2ablock\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-objc\">@property (nonatomic, copy) void (^completionBlock)(NSString *);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u8fd4\u56de\u503c\u65f6\u8c03\u7528\u8fd9\u4e2ablock\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-objc\">- (void)returnValueToPreviousController {<\/p>\n<p>    if (self.completionBlock) {<\/p>\n<p>        self.completionBlock(@&quot;Returned Data&quot;);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5728BLOCK\u4e2d\u5904\u7406\u5faa\u73af\u5f15\u7528<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528block\u65f6\u9700\u8981\u6ce8\u610f\u53ef\u80fd\u4ea7\u751f\u7684\u5faa\u73af\u5f15\u7528\u95ee\u9898\u3002\u5982\u679cblock\u5185\u90e8\u4f7f\u7528\u5230\u4e86self\uff0c\u800c\u8be5block\u53c8\u88abself\u6301\u6709\uff0c\u5c31\u4f1a\u5f62\u6210\u4e00\u4e2a\u5f3a\u5f15\u7528\u5faa\u73af\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u7528<code>__weak<\/code>\u6807\u8bb0self\u6765\u89e3\u51b3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-objc\">__weak typeof(self) weakSelf = self;<\/p>\n<p>self.myBlock = ^(NSString *result) {<\/p>\n<p>    __strong typeof(weakSelf) strongSelf = weakSelf;<\/p>\n<p>    if (strongSelf) {<\/p>\n<p>        \/\/ \u4f7f\u7528strongSelf\u6765\u907f\u514dself\u88ab\u610f\u5916\u91ca\u653e<\/p>\n<p>        strongSelf.label.text = result;<\/p>\n<p>    }<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001BLOCK\u7684\u5176\u4ed6\u7528\u9014<\/h3>\n<\/p>\n<p><p>Block\u4e0d\u4ec5\u9650\u4e8e\u503c\u7684\u4f20\u9012\uff0c\u5b83\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5f02\u6b65\u6267\u884c\u4ee3\u7801\u3001\u5904\u7406\u4e8b\u4ef6\u56de\u8c03\u4ee5\u53ca\u5728\u96c6\u5408\u7c7b\u578b\u4e2d\u8fdb\u884c\u64cd\u4f5c\uff08\u4f8b\u5982<code>NSArray<\/code>\u7684<code>enumerateObjectsUsingBlock:<\/code>\u65b9\u6cd5\uff09\u3002\u5b83\u4f7f\u5f97\u4ee3\u7801\u7684\u9605\u8bfb\u548c\u7ef4\u62a4\u53d8\u5f97\u66f4\u52a0\u65b9\u4fbf\u548c\u5f3a\u5927\u3002\u901a\u8fc7\u5728\u4e0d\u540c\u60c5\u5883\u4e0b\u5408\u7406\u5730\u4f7f\u7528block\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u63a2\u7d22\u51fa\u65e0\u9650\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cblock\u662fiOS\u5f00\u53d1\u4e2d\u4e00\u79cd\u6781\u5176\u6709\u7528\u7684\u5de5\u5177\uff0c\u65e2\u53ef\u7528\u4e8e\u6570\u636e\u4f20\u9012\uff0c\u4e5f\u53ef\u7528\u4e8e\u56de\u8c03\u548c\u5f02\u6b65\u5904\u7406\u3002\u638c\u63e1block\u7684\u58f0\u660e\u3001\u5b9a\u4e49\u3001\u4f7f\u7528\u548c\u76f8\u5173\u5185\u5b58\u7ba1\u7406\u662f\u6bcf\u4e2aiOS\u5f00\u53d1\u8005\u5fc5\u5907\u7684\u6280\u80fd\u3002\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u6307\u5bfc\uff0c\u60a8\u5e94\u80fd\u591f\u5728\u81ea\u5df1\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u987a\u5229\u5b9e\u73b0block\u4f20\u503c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728 iOS \u9879\u76ee\u4e2d\u5b9e\u73b0 block \u4f20\u9012\u6570\u503c\uff1f<\/strong><\/p>\n<p>\u4f20\u503c\u662f iOS \u5f00\u53d1\u4e2d\u5e38\u7528\u7684\u64cd\u4f5c\u4e4b\u4e00\u3002\u5b9e\u73b0 block \u4f20\u9012\u6570\u503c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 block \u5c5e\u6027\u3002\u9996\u5148\uff0c\u58f0\u660e\u4e00\u4e2a\u5e26\u6709\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684 block \u5c5e\u6027\uff0c\u5e76\u5728\u9700\u8981\u4f20\u503c\u7684\u5730\u65b9\u521d\u59cb\u5316\u8be5\u5c5e\u6027\u3002\u7136\u540e\uff0c\u901a\u8fc7\u8c03\u7528\u8be5 block \u5c5e\u6027\u6765\u4f20\u9012\u6570\u503c\u3002\u8fd9\u6837\uff0c\u5c31\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u7c7b\u4e4b\u95f4\u4f20\u9012\u6570\u503c\u4e86\u3002<\/p>\n<p><strong>iOS \u9879\u76ee\u4e2d block \u4f20\u503c\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528 block \u4f20\u503c\u5728 iOS \u9879\u76ee\u4e2d\u6709\u8bb8\u591a\u4f18\u52bf\u3002\u9996\u5148\uff0cblock \u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u7c7b\u4e4b\u95f4\u4f20\u9012\u6570\u503c\uff0c\u7b80\u5316\u4e86\u6570\u636e\u4f20\u9012\u7684\u8fc7\u7a0b\u3002\u5176\u6b21\uff0cblock \u662f\u4e00\u79cd\u5c01\u88c5\u4e86\u4ee3\u7801\u7247\u6bb5\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u643a\u5e26\u6570\u636e\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u91cd\u7528\u3002\u6b64\u5916\uff0cblock \u8fd8\u53ef\u4ee5\u5728\u5f02\u6b65\u64cd\u4f5c\u4e2d\u4f20\u9012\u6570\u636e\uff0c\u65b9\u4fbf\u5904\u7406\u591a\u7ebf\u7a0b\u4efb\u52a1\u3002\u56e0\u6b64\uff0c\u4f7f\u7528 block \u4f20\u503c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><strong>block \u4f20\u503c\u7684\u6ce8\u610f\u4e8b\u9879\u6709\u54ea\u4e9b\uff1f<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 block \u4f20\u503c\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u7ec6\u8282\u3002\u9996\u5148\uff0cblock \u4e2d\u4f7f\u7528\u7684\u5916\u90e8\u53d8\u91cf\u9700\u8981\u8fdb\u884c\u5f3a\u5f15\u7528\uff0c\u4ee5\u907f\u514d\u51fa\u73b0\u91ce\u6307\u9488\u6216\u5185\u5b58\u6cc4\u6f0f\u7684\u60c5\u51b5\u3002\u5176\u6b21\uff0c\u5982\u679c\u9700\u8981\u5728 block \u4e2d\u4fee\u6539\u5916\u90e8\u53d8\u91cf\u7684\u503c\uff0c\u9700\u8981\u4f7f\u7528 __block \u5173\u952e\u5b57\u4fee\u9970\u53d8\u91cf\u3002\u53e6\u5916\uff0c\u9700\u8981\u6ce8\u610f\u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 weak \u6216\u8005 __weak \u5173\u952e\u5b57\u6765\u4fee\u9970 block \u5185\u90e8\u7684\u53d8\u91cf\u3002\u6700\u540e\uff0c\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5efa\u8bae\u5728\u4f7f\u7528 block \u4f20\u503c\u65f6\uff0c\u7ed9 block \u5c5e\u6027\u53d6\u4e00\u4e2a\u6709\u610f\u4e49\u7684\u540d\u5b57\uff0c\u4ee5\u4fbf\u6e05\u6670\u5730\u8868\u8fbe\u4f20\u9012\u7684\u6570\u636e\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728iOS\u9879\u76ee\u4e2d\uff0cblock\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u7528\u4e8e\u5b9e\u73b0\u56de\u8c03\u548c\u6570\u636e\u4f20\u9012\u3002\u5b9e\u73b0block\u4f20\u503c\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u542b\u5b9a [&hellip;]","protected":false},"author":3,"featured_media":270981,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/270971"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=270971"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/270971\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/270981"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=270971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=270971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=270971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}