{"id":7724,"date":"2025-06-02T14:56:18","date_gmt":"2025-06-05T07:20:18","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7724"},"modified":"2025-06-05T14:56:18","modified_gmt":"2025-06-05T07:20:18","slug":"https-badgameshow-com-steven-505","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-505\/","title":{"rendered":"\u5728 SwiftUI \u4e2d\u5efa\u7acb\u73fe\u4ee3\u5316\u4e0b\u62c9\u9078\u55ae\uff1a2025 \u6700\u65b0\u6559\u5b78"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, SwiftUI, \u4e0b\u62c9\u9078\u55ae, Picker, ActionSheet\"><\/p>\n<h1>\u5728 SwiftUI \u4e2d\u5efa\u7acb\u73fe\u4ee3\u5316\u4e0b\u62c9\u9078\u55ae<\/h1>\n<p>SwiftUI \u662f Apple \u5728 WWDC 2019 \u63a8\u51fa\u7684\u5168\u65b0\u6846\u67b6\uff0c\u65e8\u5728\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u8de8\u5e73\u53f0\u7684 App\u3002\u96a8\u8457 SwiftUI \u7684\u4e0d\u65b7\u66f4\u65b0\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u52a0\u8f15\u9b06\u5730\u64b0\u5beb UI \u7a0b\u5f0f\u78bc\u3002\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u5728 SwiftUI \u4e2d\u4f7f\u7528\u4e0b\u62c9\u9078\u55ae\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u4f5c\u7bc4\u4f8b\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u4ee5\u4fbf\u65bc\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u5229\u7528\u9019\u500b\u5f37\u5927\u7684\u5de5\u5177\u3002<\/p>\n<h2>\u4f7f\u7528 Picker \u5efa\u7acb\u4e0b\u62c9\u9078\u55ae<\/h2>\n<p>\u5728 SwiftUI \u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 <code>Picker<\/code> \u4f86\u5efa\u7acb\u4e0b\u62c9\u9078\u55ae\u3002<code>Picker<\/code> \u5141\u8a31\u958b\u767c\u8005\u6307\u5b9a\u9078\u9805\uff0c\u4e26\u8b93\u4f7f\u7528\u8005\u9078\u64c7\u4ed6\u5011\u60f3\u8981\u7684\u9078\u64c7\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u986f\u793a\u5982\u4f55\u4f7f\u7528 <code>Picker<\/code> \u4f86\u8b93\u4f7f\u7528\u8005\u9078\u64c7\u6027\u5225\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">struct ContentView: View {\n    @State private var selectedGender = 0\n\n    var body: some View {\n        VStack {\n            Picker(selection: $selectedGender, label: Text(\"Gender\")) {\n                Text(\"Male\").tag(0)\n                Text(\"Female\").tag(1)\n            }\n            .pickerStyle(MenuPickerStyle()) \/\/ \u4f7f\u7528 MenuPickerStyle \u4f86\u986f\u793a\u4e0b\u62c9\u9078\u55ae\n            .padding()\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>Picker<\/code> \u4f86\u5efa\u7acb\u4e0b\u62c9\u9078\u55ae\u3002\u900f\u904e <code>selectedGender<\/code> \u4f86\u5132\u5b58\u9078\u64c7\u7684\u7d50\u679c\u3002\u6211\u5011\u4f7f\u7528 <code>Text<\/code> \u4f86\u5b9a\u7fa9\u9078\u9805\uff0c\u4e26\u900f\u904e <code>tag<\/code> \u4f86\u6307\u5b9a\u6bcf\u500b\u9078\u9805\u7684\u503c\u3002\u9019\u88e1\uff0c\u6211\u5011\u9078\u64c7\u4e86 <code>MenuPickerStyle<\/code> \u4f5c\u70ba\u4e0b\u62c9\u9078\u55ae\u7684\u6a23\u5f0f\uff0c\u9019\u662f SwiftUI 2025 \u7248\u672c\u4e2d\u7684\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u4f7f\u7528 ActionSheet \u5efa\u7acb\u4e0b\u62c9\u9078\u55ae<\/h2>\n<p>\u9664\u4e86 <code>Picker<\/code>\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u4f7f\u7528 <code>ActionSheet<\/code> \u4f86\u5efa\u7acb\u5f48\u51fa\u5f0f\u7684\u9078\u55ae\uff0c\u8b93\u4f7f\u7528\u8005\u5728\u5f48\u51fa\u8996\u7a97\u4e2d\u9078\u64c7\u9078\u9805\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 <code>ActionSheet<\/code> \u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">struct ContentView: View {\n    @State private var selectedColor = 0\n    @State private var showActionSheet = false\n\n    var body: some View {\n        VStack {\n            Button(action: {\n                self.showActionSheet = true\n            }) {\n                Text(\"Select Color\")\n            }\n            .actionSheet(isPresented: $showActionSheet) {\n                ActionSheet(title: Text(\"Select Color\"), buttons: [\n                    .default(Text(\"Red\"), action: { self.selectedColor = 0 }),\n                    .default(Text(\"Green\"), action: { self.selectedColor = 1 }),\n                    .default(Text(\"Blue\"), action: { self.selectedColor = 2 }),\n                    .cancel()\n                ])\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>ActionSheet<\/code> \u4f86\u5448\u73fe\u5f48\u51fa\u5f0f\u9078\u55ae\u3002\u900f\u904e <code>showActionSheet<\/code> \u4f86\u63a7\u5236\u4f55\u6642\u986f\u793a\u9078\u55ae\uff0c\u4e26\u4f7f\u7528 <code>buttons<\/code> \u9663\u5217\u4f86\u5b9a\u7fa9\u53ef\u9078\u7684\u984f\u8272\u9078\u9805\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u5be6\u4f5c\u4e0b\u62c9\u9078\u55ae\u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u9078\u9805\u4e0d\u986f\u793a\uff1a<\/strong>\u78ba\u4fdd\u9078\u9805\u4e2d\u7684 <code>tag<\/code> \u8207 <code>selection<\/code> \u8b8a\u6578\u6b63\u78ba\u5c0d\u61c9\u3002<\/li>\n<li><strong>UI \u7121\u6cd5\u66f4\u65b0\uff1a<\/strong>\u78ba\u8a8d\u4f60\u4f7f\u7528 <code>@State<\/code> \u6216 <code>@Binding<\/code> \u6b63\u78ba\u5730\u7ba1\u7406\u72c0\u614b\u3002<\/li>\n<\/ul>\n<p>\u6b64\u5916\uff0c\u5efa\u8b70\u4f7f\u7528 <code>MenuPickerStyle<\/code> \u6216 <code>ActionSheet<\/code>\uff0c\u9019\u4e0d\u50c5\u80fd\u63d0\u5347\u4f7f\u7528\u8005\u9ad4\u9a57\uff0c\u9084\u80fd\u8b93\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u770b\u8d77\u4f86\u66f4\u52a0\u73fe\u4ee3\u5316\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u6027\u5225\u548c\u984f\u8272\u9078\u64c7\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528\u4e0b\u62c9\u9078\u55ae\u4f86\u5be6\u73fe\u5404\u7a2e\u529f\u80fd\uff0c\u4f8b\u5982\u9078\u64c7\u570b\u5bb6\u3001\u8a9e\u8a00\u6216\u5176\u4ed6\u8a2d\u5b9a\u9078\u9805\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u80fd\u5920\u589e\u5f37\u4f7f\u7528\u8005\u4e92\u52d5\u6027\uff0c\u63d0\u5347\u6574\u9ad4\u4f7f\u7528\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\u4e0b\u62c9\u9078\u55ae_1676961843.072351.webp\" alt=\"\u5728 SwiftUI \u4e2d\u4f7f\u7528\u4e0b\u62c9\u9078\u55ae\" title=\"SwiftUI, \u4e0b\u62c9\u9078\u55ae, Picker, \u63a7\u5236\u9805, \u9078\u55ae\" 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\u81ea\u8a02\u4e0b\u62c9\u9078\u55ae\u7684\u6a23\u5f0f\uff1f<\/h3>\n<p>\u4f60\u53ef\u4ee5\u900f\u904e\u9078\u64c7\u4e0d\u540c\u7684 <code>pickerStyle<\/code> \u8a2d\u7f6e\uff0c\u4f8b\u5982 <code>MenuPickerStyle<\/code> \u6216 <code>SegmentedPickerStyle<\/code>\uff0c\u4f86\u81ea\u8a02\u4e0b\u62c9\u9078\u55ae\u7684\u5916\u89c0\u3002<\/p>\n<h3>2. \u5728 SwiftUI \u4e2d\u5982\u4f55\u8655\u7406\u9078\u64c7\u6539\u8b8a\u7684\u4e8b\u4ef6\uff1f<\/h3>\n<p>\u4f7f\u7528 <code>@State<\/code> \u4f86\u5132\u5b58\u9078\u64c7\u7684\u72c0\u614b\uff0c\u4e26\u5728 <code>Picker<\/code> \u6216 <code>ActionSheet<\/code> \u4e2d\u5c07\u5176\u7d81\u5b9a\uff0c\u9019\u6a23\u4f60\u5c31\u53ef\u4ee5\u5728\u9078\u64c7\u6539\u8b8a\u6642\u57f7\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002<\/p>\n<h3>3. SwiftUI \u7684\u4e0b\u62c9\u9078\u55ae\u53ef\u4ee5\u652f\u63f4\u591a\u9078\u55ce\uff1f<\/h3>\n<p>\u5728 SwiftUI \u4e2d\uff0c<code>Picker<\/code> \u672c\u8eab\u4e0d\u652f\u63f4\u591a\u9078\uff0c\u4f46\u53ef\u4ee5\u900f\u904e\u81ea\u8a02 UI \u6216\u4f7f\u7528 <code>List<\/code> \u914d\u5408 <code>Toggle<\/code> \u4f86\u5be6\u73fe\u591a\u9078\u7684\u529f\u80fd\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728SwiftUI\u4e2d\u4f7f\u7528\u4e0b\u62c9\u9078\u55ae\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5efa\u7acb\u51fa\u7c21\u55ae\u53c8\u7f8e\u89c0\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0eSwiftUI\u4e2d\u7684Picker\u63a7\u5236\u9805\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u5efa\u7acb\u4e0b\u62c9\u9078\u55ae\u3002<\/p>\n","protected":false},"author":1,"featured_media":7723,"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-7724","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\u4e0b\u62c9\u9078\u55ae_1676961843.072351.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-20A","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7724","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=7724"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7724\/revisions"}],"predecessor-version":[{"id":13158,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7724\/revisions\/13158"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7723"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}