{"id":7726,"date":"2025-05-29T13:26:11","date_gmt":"2025-06-05T05:36:10","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7726"},"modified":"2025-06-05T13:26:11","modified_gmt":"2025-06-05T05:36:10","slug":"https-badgameshow-com-steven-506","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-506\/","title":{"rendered":"\u5728 SwiftUI \u4e2d\u4f7f\u7528\u8b66\u544a\u8996\u5716\u7684\u5b8c\u6574\u6307\u5357\uff082025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff09"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, SwiftUI, \u8b66\u544a\u8996\u5716, \u4f7f\u7528\u8b66\u544a\u8996\u5716, \u958b\u767c, App\u958b\u767c\"><\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>SwiftUI \u662f Apple \u5728 WWDC 2019 \u63a8\u51fa\u7684\u5f37\u5927\u6846\u67b6\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u5275\u5efa\u8de8\u5e73\u53f0\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u5728 SwiftUI \u4e2d\uff0c\u8b66\u544a\u8996\u5716\uff08Alert\uff09\u662f\u4e00\u500b\u91cd\u8981\u7684\u5143\u4ef6\uff0c\u80fd\u5920\u6709\u6548\u5730\u63d0\u793a\u4f7f\u7528\u8005\u4e26\u589e\u5f37\u61c9\u7528\u7684\u4e92\u52d5\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 SwiftUI \u4e2d\u4f7f\u7528\u8b66\u544a\u8996\u5716\uff0c\u4e26\u63d0\u4f9b\u6700\u65b0\u7684\u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5efa\u7acb\u8b66\u544a\u8996\u5716<\/h2>\n<p>\u5728 SwiftUI \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>Alert<\/code> \u7d50\u69cb\u4f86\u5275\u5efa\u8b66\u544a\u8996\u5716\u3002\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u5c55\u793a\u5982\u4f55\u5b9a\u7fa9\u4e00\u500b\u57fa\u672c\u7684\u8b66\u544a\u8996\u5716\uff0c\u5305\u62ec\u6a19\u984c\u3001\u8a0a\u606f\u548c\u6309\u9215\u9078\u9805\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let alert = Alert(title: Text(\"Hello World\"), \n                  message: Text(\"This is an alert\"), \n                  primaryButton: .default(Text(\"OK\")), \n                  secondaryButton: .cancel())\n<\/code><\/pre>\n<h2>\u986f\u793a\u8b66\u544a\u8996\u5716<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5c07\u8b66\u544a\u8996\u5716\u8207\u6211\u5011\u7684\u8996\u5716\u7d81\u5b9a\u3002\u4ee5\u4e0b\u793a\u7bc4\u4e86\u5982\u4f55\u4f7f\u7528 <code>$showingAlert<\/code> \u72c0\u614b\u8b8a\u6578\u4f86\u63a7\u5236\u8b66\u544a\u8996\u5716\u7684\u986f\u793a\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">@State private var showingAlert = false\n\nVStack {\n    Text(\"Hello World\")\n        .alert(isPresented: $showingAlert) {\n            alert\n        }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u7576 <code>$showingAlert<\/code> \u7684\u503c\u70ba <code>true<\/code> \u6642\uff0c\u8b66\u544a\u8996\u5716\u6703\u986f\u793a\u51fa\u4f86\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u624b\u52e2\u4f86\u89f8\u767c\u8b66\u544a\u7684\u986f\u793a\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">VStack {\n    Text(\"Hello World\")\n        .alert(isPresented: $showingAlert) {\n            alert\n        }\n        .onTapGesture {\n            self.showingAlert = true\n        }\n}\n<\/code><\/pre>\n<h2>\u9032\u968e\u4f7f\u7528\uff1a\u63a7\u5236\u8b66\u544a\u8996\u5716\u7684\u986f\u793a\u8207\u6d88\u5931<\/h2>\n<p>\u9664\u4e86\u4f7f\u7528 <code>$showingAlert<\/code> \u4f86\u63a7\u5236\u986f\u793a\uff0c\u9084\u53ef\u4ee5\u5229\u7528 <code>presentationMode<\/code> \u4f86\u7ba1\u7406\u8996\u5716\u7684\u72c0\u614b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">@Environment(\\.presentationMode) var presentationMode\n\nVStack {\n    Text(\"Hello World\")\n        .alert(isPresented: $showingAlert) {\n            alert\n        }\n        .onTapGesture {\n            self.showingAlert = true\n        }\n        .onDisappear {\n            self.presentationMode.wrappedValue.dismiss()\n        }\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u5728\u8996\u5716\u6d88\u5931\u6642\u96b1\u85cf\u8b66\u544a\u8996\u5716\uff0c\u63d0\u4f9b\u66f4\u6d41\u66a2\u7684\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528\u8b66\u544a\u8996\u5716\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e7e\u500b\u5e38\u898b\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u8b66\u544a\u8996\u5716\u4e0d\u986f\u793a\uff1a<\/strong>\u78ba\u4fdd <code>$showingAlert<\/code> \u8b8a\u6578\u88ab\u6b63\u78ba\u8a2d\u7f6e\u70ba <code>true<\/code>\u3002<\/li>\n<li><strong>\u591a\u500b\u8b66\u544a\u8996\u5716\u7684\u7ba1\u7406\uff1a<\/strong>\u5982\u679c\u9700\u8981\u986f\u793a\u591a\u500b\u8b66\u544a\u8996\u5716\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u4e0d\u540c\u7684\u72c0\u614b\u8b8a\u6578\u4f86\u63a7\u5236\u6bcf\u500b\u8b66\u544a\u7684\u986f\u793a\u3002<\/li>\n<li><strong>\u6309\u9215\u53cd\u61c9\u4e0d\u6b63\u78ba\uff1a<\/strong>\u6aa2\u67e5\u6309\u9215\u7684\u56de\u8abf\u662f\u5426\u88ab\u6b63\u78ba\u8a2d\u7f6e\uff0c\u4e26\u78ba\u4fdd\u6c92\u6709\u5176\u4ed6\u624b\u52e2\u6216\u72c0\u614b\u5e72\u64fe\u6309\u9215\u7684\u884c\u70ba\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u8b66\u544a\u8996\u5716\u4e0d\u50c5\u9650\u65bc\u986f\u793a\u7c21\u55ae\u7684\u63d0\u793a\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u78ba\u8a8d\u64cd\u4f5c\u3001\u986f\u793a\u932f\u8aa4\u8a0a\u606f\u6216\u63d0\u4f9b\u9078\u9805\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728\u522a\u9664\u9805\u76ee\u4e4b\u524d\u986f\u793a\u78ba\u8a8d\u8b66\u544a\uff0c\u6216\u5728\u767c\u751f\u932f\u8aa4\u6642\u544a\u8a34\u7528\u6236\u5982\u4f55\u89e3\u6c7a\u554f\u984c\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u5728 SwiftUI \u4e2d\u4f7f\u7528 <strong>Alert<\/strong> \u662f\u63d0\u5347\u61c9\u7528\u4e92\u52d5\u6027\u7684\u91cd\u8981\u5de5\u5177\u3002\u900f\u904e\u672c\u6587\u4ecb\u7d39\u7684\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u958b\u767c\u8005\u80fd\u5920\u6709\u6548\u5730\u5229\u7528\u8b66\u544a\u8996\u5716\u5f37\u5316\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/\u5728-SwiftUI-\u4e2d\u4f7f\u7528\u8b66\u544a\u8996\u5716_1676961951.411561.webp\" alt=\"\u5728 SwiftUI \u4e2d\u4f7f\u7528\u8b66\u544a\u8996\u5716\" title=\"SwiftUI, \u8b66\u544a\u8996\u5716, \u63d0\u793a, \u7528\u6236\u9ad4\u9a57\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5728 SwiftUI \u4e2d\u986f\u793a\u591a\u500b\u8b66\u544a\u8996\u5716\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u70ba\u6bcf\u500b\u8b66\u544a\u8996\u5716\u5275\u5efa\u7368\u7acb\u7684\u72c0\u614b\u8b8a\u6578\uff0c\u4e26\u5728\u9700\u8981\u986f\u793a\u6642\u8a2d\u7f6e\u76f8\u61c9\u7684\u8b8a\u6578\u70ba <code>true<\/code>\u3002<\/p>\n<h3>2. \u8b66\u544a\u8996\u5716\u7684\u6309\u9215\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u70ba <code>Alert<\/code> \u7684\u6309\u9215\u8a2d\u7f6e\u81ea\u5b9a\u7fa9\u7684\u884c\u70ba\uff0c\u4f8b\u5982\u57f7\u884c\u7279\u5b9a\u7684\u64cd\u4f5c\u6216\u5c0e\u822a\u5230\u5176\u4ed6\u8996\u5716\u3002<\/p>\n<h3>3. \u5982\u4f55\u8655\u7406\u8b66\u544a\u8996\u5716\u4e2d\u7684\u932f\u8aa4\u8a0a\u606f\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5728\u8b66\u544a\u8996\u5716\u7684\u8a0a\u606f\u90e8\u5206\u986f\u793a\u932f\u8aa4\u8cc7\u8a0a\uff0c\u4e26\u63d0\u4f9b\u76f8\u61c9\u7684\u89e3\u6c7a\u65b9\u6848\u6216\u63d0\u793a\u7528\u6236\u5982\u4f55\u9032\u884c\u4e0b\u4e00\u6b65\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728SwiftUI\u4e2d\u4f7f\u7528\u8b66\u544a\u8996\u5716\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u6dfb\u52a0\u8b66\u544a\u63d0\u793a\uff0c\u4ee5\u63d0\u9ad8\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":1,"featured_media":7725,"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-7726","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\u8b66\u544a\u8996\u5716_1676961951.411561.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-20C","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7726","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=7726"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7726\/revisions"}],"predecessor-version":[{"id":13219,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7726\/revisions\/13219"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7725"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}