{"id":7708,"date":"2025-05-30T13:28:15","date_gmt":"2025-06-05T05:48:15","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7708"},"modified":"2025-06-05T13:28:15","modified_gmt":"2025-06-05T05:48:15","slug":"https-badgameshow-com-steven-497","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-497\/","title":{"rendered":"\u5728 SwiftUI \u4e2d\u4f7f\u7528\u6a21\u6001\u89c6\u56fe\u7684\u5b8c\u6574\u6307\u5357\u30102025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3011"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, SwiftUI, \u6a21\u6001\u89c6\u56fe, iOS \u5f00\u53d1, Swift \u6559\u5b66\"><\/p>\n<h1>\u5728 SwiftUI \u4e2d\u4f7f\u7528\u6a21\u6001\u89c6\u56fe\u7684\u5b8c\u6574\u6307\u5357<\/h1>\n<p>SwiftUI \u662f Apple \u5728 WWDC 2019 \u63a8\u51fa\u7684\u65b0\u6846\u67b6\uff0c\u5b83\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u5feb\u901f\u521b\u5efa\u539f\u751f iOS\u3001macOS\u3001tvOS \u548c watchOS \u5e94\u7528\u7a0b\u5e8f\u3002\u5728 SwiftUI \u4e2d\uff0c\u6a21\u6001\u89c6\u56fe\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\uff0c\u80fd\u591f\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\uff0c\u4f7f\u7528\u6237\u5728\u4e0d\u79bb\u5f00\u5f53\u524d\u89c6\u56fe\u7684\u60c5\u51b5\u4e0b\u5b8c\u6210\u66f4\u591a\u4efb\u52a1\u3002<\/p>\n<h2>\u4ec0\u4e48\u662f\u6a21\u6001\u89c6\u56fe\uff1f<\/h2>\n<p>\u6a21\u6001\u89c6\u56fe\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u89c6\u56fe\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u67e5\u770b\u53e6\u4e00\u4e2a\u89c6\u56fe\u7684\u5185\u5bb9\uff0c\u800c\u4e0d\u9700\u8981\u79bb\u5f00\u5f53\u524d\u89c6\u56fe\u3002\u901a\u8fc7\u6a21\u6001\u89c6\u56fe\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u5e94\u7528\u4e2d\u5d4c\u5165\u66f4\u591a\u529f\u80fd\uff0c\u63d0\u5347\u7528\u6237\u7684\u64cd\u4f5c\u6548\u7387\u3002<\/p>\n<h2>\u5982\u4f55\u5728 SwiftUI \u4e2d\u4f7f\u7528\u6a21\u6001\u89c6\u56fe<\/h2>\n<p>\u8981\u5728 SwiftUI \u4e2d\u5b9e\u73b0\u6a21\u6001\u89c6\u56fe\uff0c\u9996\u5148\u9700\u8981\u58f0\u660e\u4e00\u4e2a <strong>@State<\/strong> \u7684 <strong>Bool<\/strong> \u7c7b\u578b\u53d8\u91cf\uff0c\u7528\u4e8e\u63a7\u5236\u6a21\u6001\u89c6\u56fe\u7684\u663e\u793a\u72b6\u6001\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">@State private var showModal = false<\/code><\/pre>\n<p>\u63a5\u7740\uff0c\u5728\u9700\u8981\u5c55\u793a\u6a21\u6001\u89c6\u56fe\u7684\u6309\u94ae\u4e2d\uff0c\u4f7f\u7528 <strong>.sheet<\/strong> \u4fee\u9970\u7b26\u6765\u5b9e\u73b0\u6a21\u6001\u89c6\u56fe\u7684\u663e\u793a\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">Button(action: {\n    self.showModal = true\n}) {\n    Text(\"\u663e\u793a\u6a21\u6001\u89c6\u56fe\")\n}\n.sheet(isPresented: $showModal) {\n    ModalView()\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u7528\u6237\u70b9\u51fb\u6309\u94ae\u540e\uff0c<strong>showModal<\/strong> \u7684\u503c\u4f1a\u88ab\u8bbe\u7f6e\u4e3a <strong>true<\/strong>\uff0c\u800c <strong>.sheet<\/strong> \u4fee\u9970\u7b26\u4f1a\u6839\u636e <strong>showModal<\/strong> \u7684\u503c\u6765\u51b3\u5b9a\u662f\u5426\u5c55\u793a\u6a21\u6001\u89c6\u56fe\u3002<\/p>\n<h2>\u6a21\u6001\u89c6\u56fe\u7684\u5b9e\u73b0<\/h2>\n<p>\u4e0b\u9762\u662f\u6a21\u6001\u89c6\u56fe\u7684\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">struct ModalView: View {\n    @Environment(\\.presentationMode) var presentationMode\n\n    var body: some View {\n        VStack {\n            Text(\"\u8fd9\u662f\u4e00\u4e2a\u6a21\u6001\u89c6\u56fe\")\n            Button(action: {\n                self.presentationMode.wrappedValue.dismiss()\n            }) {\n                Text(\"\u5173\u95ed\")\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <strong>presentationMode<\/strong> \u6765\u63a7\u5236\u6a21\u6001\u89c6\u56fe\u7684\u5173\u95ed\u3002\u5f53\u7528\u6237\u70b9\u51fb\u201c\u5173\u95ed\u201d\u6309\u94ae\u65f6\uff0c<strong>presentationMode<\/strong> \u7684 <strong>dismiss()<\/strong> \u65b9\u6cd5\u4f1a\u88ab\u8c03\u7528\uff0c\u4ece\u800c\u5173\u95ed\u6a21\u6001\u89c6\u56fe\u3002<\/p>\n<h2>\u9519\u8bef\u6392\u9664<\/h2>\n<p>\u5982\u679c\u6a21\u6001\u89c6\u56fe\u65e0\u6cd5\u6b63\u786e\u663e\u793a\uff0c\u8bf7\u68c0\u67e5\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u786e\u4fdd <strong>showModal<\/strong> \u53d8\u91cf\u7684\u521d\u59cb\u503c\u4e3a <strong>false<\/strong>\u3002<\/li>\n<li>\u68c0\u67e5 <strong>.sheet<\/strong> \u4fee\u9970\u7b26\u662f\u5426\u6b63\u786e\u8fde\u63a5\u5230\u6309\u94ae\u7684 <strong>isPresented<\/strong> \u5c5e\u6027\u3002<\/li>\n<li>\u786e\u8ba4\u6a21\u6001\u89c6\u56fe\u7684\u7ed3\u6784\u4f53\u662f\u5426\u9075\u5faa <strong>View<\/strong> \u534f\u8bae\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u5e94\u7528<\/h2>\n<p>\u6a21\u6001\u89c6\u56fe\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u6587\u672c\u4fe1\u606f\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u8868\u5355\u3001\u56fe\u7247\u9009\u62e9\u5668\u7b49\u590d\u6742\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6a21\u6001\u89c6\u56fe\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<ul>\n<li>\u7528\u6237\u767b\u5f55\/\u6ce8\u518c\u754c\u9762<\/li>\n<li>\u8bbe\u7f6e\u6216\u914d\u7f6e\u9009\u9879<\/li>\n<li>\u6570\u636e\u8f93\u5165\u8868\u5355<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/\u5728-SwiftUI-\u4e2d\u4f7f\u7528\u6a21\u6001\u8996\u5716_1676960989.13364.webp\" alt=\"\u5728 SwiftUI \u4e2d\u4f7f\u7528\u6a21\u6001\u89c6\u56fe\" title=\"SwiftUI \u4e2d\u7684\u6a21\u6001\u89c6\u56fe\u793a\u4f8b\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u5728 SwiftUI \u4e2d\uff0c\u6a21\u6001\u89c6\u56fe\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u63d0\u4f9b\u66f4\u52a0\u4e30\u5bcc\u7684\u7528\u6237\u4f53\u9a8c\u3002\u901a\u8fc7\u7b80\u5355\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u6a21\u6001\u89c6\u56fe\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u5b9a\u5236\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u89c1\u95ee\u9898\u89e3\u7b54\uff09<\/h2>\n<h3>1. SwiftUI \u7684\u6a21\u6001\u89c6\u56fe\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5417\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u6a21\u6001\u89c6\u56fe\u5b8c\u5168\u53ef\u4ee5\u81ea\u5b9a\u4e49\uff0c\u60a8\u53ef\u4ee5\u6dfb\u52a0\u4efb\u4f55\u89c6\u56fe\u7ec4\u4ef6\uff0c\u5305\u62ec\u6587\u672c\u3001\u6309\u94ae\u3001\u56fe\u7247\u7b49\uff0c\u6765\u6ee1\u8db3\u5e94\u7528\u7684\u9700\u6c42\u3002<\/p>\n<h3>2. \u5982\u4f55\u5728\u6a21\u6001\u89c6\u56fe\u4e2d\u5904\u7406\u6570\u636e\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u7ed1\u5b9a\u6570\u636e\u548c\u4f7f\u7528 <strong>@State<\/strong> \u6216 <strong>@Binding<\/strong> \u5c5e\u6027\u6765\u5728\u4e3b\u89c6\u56fe\u548c\u6a21\u6001\u89c6\u56fe\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002<\/p>\n<h3>3. \u6a21\u6001\u89c6\u56fe\u4f1a\u5f71\u54cd\u5e94\u7528\u6027\u80fd\u5417\uff1f<\/h3>\n<p>\u6a21\u6001\u89c6\u56fe\u672c\u8eab\u4e0d\u4f1a\u663e\u8457\u5f71\u54cd\u6027\u80fd\uff0c\u4f46\u5728\u89c6\u56fe\u4e2d\u6dfb\u52a0\u590d\u6742\u7684\u7ec4\u4ef6\u6216\u52a8\u753b\u65f6\uff0c\u53ef\u80fd\u4f1a\u9020\u6210\u4e00\u5b9a\u7684\u6027\u80fd\u5f00\u9500\u3002\u786e\u4fdd\u4f18\u5316\u89c6\u56fe\u7ed3\u6784\u548c\u52a8\u753b\u6548\u679c\uff0c\u53ef\u4ee5\u63d0\u5347\u6574\u4f53\u6027\u80fd\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5728SwiftUI\u4e2d\uff0c\u6a21\u6001\u8996\u5716\u53ef\u4ee5\u8b93\u60a8\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u63d0\u4f9b\u4e00\u500b\u65b0\u7684\u8996\u5716\uff0c\u4e26\u5728\u7528\u6236\u5b8c\u6210\u5176\u5de5\u4f5c\u5f8c\u8fd4\u56de\u5230\u539f\u59cb\u8996\u5716\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728SwiftUI\u4e2d\u4f7f\u7528\u6a21\u6001\u8996\u5716\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\u7d66\u7528\u6236\u3002<\/p>\n","protected":false},"author":1,"featured_media":7707,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-7708","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/\u5728-SwiftUI-\u4e2d\u4f7f\u7528\u6a21\u6001\u8996\u5716_1676960989.13364.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-20k","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=7708"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7708\/revisions"}],"predecessor-version":[{"id":13267,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7708\/revisions\/13267"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7707"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}