{"id":7710,"date":"2025-06-03T13:31:13","date_gmt":"2025-06-05T05:49:13","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7710"},"modified":"2025-06-05T13:31:13","modified_gmt":"2025-06-05T05:49:13","slug":"https-badgameshow-com-steven-498","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-498\/","title":{"rendered":"2025 \u5e74\u6700\u65b0\u6307\u5357\uff1a\u5728 SwiftUI \u4e2d\u4f7f\u7528\u9078\u64c7\u5668\uff08Picker\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, SwiftUI, \u9078\u64c7\u5668, Picker, \u6559\u5b78, iOS \u958b\u767c, 2025\"><\/p>\n<h1>2025 \u5e74\u6700\u65b0\u6307\u5357\uff1a\u5728 SwiftUI \u4e2d\u4f7f\u7528\u9078\u64c7\u5668\uff08Picker\uff09<\/h1>\n<p>SwiftUI \u662f Apple \u5728 WWDC 2019 \u63a8\u51fa\u7684\u5275\u65b0\u6846\u67b6\uff0c\u65e8\u5728\u7c21\u5316 iOS\u3001macOS\u3001tvOS \u548c watchOS \u61c9\u7528\u7a0b\u5f0f\u7684\u958b\u767c\u904e\u7a0b\u3002\u4f5c\u70ba\u9019\u4e00\u6846\u67b6\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u5143\u4ef6\uff0c\u9078\u64c7\u5668\uff08Picker\uff09\u4f7f\u5f97\u7528\u6236\u53ef\u4ee5\u65b9\u4fbf\u5730\u5f9e\u4e00\u7cfb\u5217\u9078\u9805\u4e2d\u9032\u884c\u9078\u64c7\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u9078\u64c7\u5668\uff08Picker\uff09\uff1f<\/h2>\n<p>\u9078\u64c7\u5668\uff08Picker\uff09\u662f\u4e00\u500b SwiftUI \u5143\u4ef6\uff0c\u5141\u8a31\u958b\u767c\u8005\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u63d0\u4f9b\u4e00\u500b\u53ef\u9078\u64c7\u7684\u5217\u8868\u3002\u9019\u500b\u5143\u4ef6\u53ef\u4ee5\u7528\u4f86\u66ff\u4ee3\u50b3\u7d71\u7684 iOS \u4e2d\u7684 UIPickerView \u6216 macOS \u4e2d\u7684 NSPopUpButton\uff0c\u4e26\u63d0\u4f9b\u66f4\u73fe\u4ee3\u5316\u7684\u7528\u6236\u4ecb\u9762\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528\u9078\u64c7\u5668\uff08Picker\uff09\uff1f<\/h2>\n<p>\u4f7f\u7528\u9078\u64c7\u5668\uff08Picker\uff09\u5728 SwiftUI \u4e2d\u975e\u5e38\u7c21\u55ae\u3002\u60a8\u53ea\u9700\u4f7f\u7528 <code>Picker<\/code> \u95dc\u9375\u5b57\uff0c\u4e26\u63d0\u4f9b\u4e00\u500b <code>Label<\/code> \u548c <code>ForEach<\/code> \u4f86\u986f\u793a\u53ef\u9078\u64c7\u7684\u9805\u76ee\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u756b\u9762\u4e0a\u986f\u793a\u4e00\u500b\u9078\u64c7\u5668\uff0c\u8b93\u4f7f\u7528\u8005\u9078\u64c7\u984f\u8272\uff1a<\/p>\n<pre><code>struct ContentView: View {\n    @State private var selectedColor = 0\n    var colors = [\"Red\", \"Green\", \"Blue\"]\n\n    var body: some View {\n        Picker(selection: $selectedColor, label: Text(\"\u8acb\u9078\u64c7\u984f\u8272\")) {\n            ForEach(0 ..< colors.count) { index in\n                Text(self.colors[index])\n            }\n        }\n        .pickerStyle(MenuPickerStyle()) \/\/ \u9078\u64c7\u5668\u6a23\u5f0f\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>Picker<\/code> \u4f86\u5efa\u7acb\u4e00\u500b\u9078\u64c7\u5668\uff0c\u4e26\u900f\u904e <code>ForEach<\/code> \u4f86\u986f\u793a\u53ef\u9078\u9805\u76ee\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6211\u5011\u4f7f\u7528 <code>$selectedColor<\/code> \u4f86\u7d81\u5b9a\u9078\u64c7\u7684\u984f\u8272\u7d22\u5f15\u3002<\/p>\n<h2>\u9032\u968e\u4f7f\u7528\uff1a\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u6a23\u5f0f<\/h2>\n<p>SwiftUI \u5141\u8a31\u958b\u767c\u8005\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u7684\u6a23\u5f0f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>pickerStyle<\/code> \u65b9\u6cd5\u4f86\u9078\u64c7\u4e0d\u540c\u7684\u6a23\u5f0f\uff0c\u4f8b\u5982 <code>MenuPickerStyle<\/code> \u6216 <code>WheelPickerStyle<\/code>\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u9019\u4e9b\u6a23\u5f0f\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>.pickerStyle(WheelPickerStyle()) \/\/ \u4f7f\u7528\u8f2a\u5f0f\u9078\u64c7\u5668\u6a23\u5f0f\n<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u5275\u5efa\u51fa\u4e0d\u540c\u7684\u7528\u6236\u4e92\u52d5\u9ad4\u9a57\uff0c\u6839\u64da\u61c9\u7528\u7684\u9700\u6c42\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u7684\u5916\u89c0\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528\u9078\u64c7\u5668\u6642\uff0c\u82e5\u767c\u751f\u9078\u64c7\u4e0d\u6b63\u78ba\u6216\u7121\u6cd5\u986f\u793a\u9078\u9805\u7684\u60c5\u6cc1\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<br \/>\n- \u78ba\u4fdd <code>colors<\/code> \u9663\u5217\u5df2\u6b63\u78ba\u521d\u59cb\u5316\u3002<br \/>\n- \u78ba\u8a8d <code>ForEach<\/code> \u7684\u7bc4\u570d\u6b63\u78ba\uff0c\u907f\u514d\u8d8a\u754c\u932f\u8aa4\u3002<br \/>\n- \u78ba\u4fdd <code>selection<\/code> \u8b8a\u6578\u5df2\u6b63\u78ba\u7d81\u5b9a\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9078\u64c7\u5668\u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u984f\u8272\u9078\u64c7\uff0c\u9084\u53ef\u4ee5\u64f4\u5c55\u5230\u5176\u4ed6\u985e\u578b\u7684\u6578\u64da\uff0c\u4f8b\u5982\u65e5\u671f\u3001\u6578\u5b57\u6216\u81ea\u5b9a\u7fa9\u7269\u4ef6\u3002\u9019\u4f7f\u5f97\u9078\u64c7\u5668\u6210\u70ba\u4e00\u500b\u9748\u6d3b\u7684\u5143\u4ef6\uff0c\u80fd\u5920\u61c9\u7528\u65bc\u591a\u7a2e\u5834\u666f\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 SwiftUI \u4e2d\u4f7f\u7528\u9078\u64c7\u5668\uff08Picker\uff09\u3002\u9019\u500b\u5143\u4ef6\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u7c21\u6f54\u5730\u63d0\u4f9b\u53ef\u9078\u64c7\u7684\u5217\u8868\uff0c\u4e26\u4e14\u6613\u65bc\u4f7f\u7528\u548c\u81ea\u5b9a\u7fa9\u3002\u900f\u904e\u9019\u4e9b\u7bc4\u4f8b\u548c\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5728\u61c9\u7528\u4e2d\u61c9\u7528\u9078\u64c7\u5668\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/\u5728-SwiftUI-\u4e2d\u4f7f\u7528\u9078\u64c7\u5668_1676961115.936333.webp\" alt=\"\u5728 SwiftUI \u4e2d\u4f7f\u7528\u9078\u64c7\u5668\" title=\"SwiftUI, \u9078\u64c7\u5668, \u61c9\u7528\u7a0b\u5e8f, \u529f\u80fd, \u5f37\u5927\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5728 SwiftUI \u4e2d\uff0c\u5982\u4f55\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u7684\u6a23\u5f0f\uff1f<\/h3>\n<p>A1: \u60a8\u53ef\u4ee5\u4f7f\u7528 <code>pickerStyle<\/code> \u65b9\u6cd5\u4f86\u9078\u64c7\u4e0d\u540c\u7684\u6a23\u5f0f\uff0c\u4f8b\u5982 <code>MenuPickerStyle<\/code> \u6216 <code>WheelPickerStyle<\/code>\u3002<\/p>\n<h3>Q2: \u9078\u64c7\u5668\u7684 <code>selection<\/code> \u53c3\u6578\u61c9\u8a72\u5982\u4f55\u8a2d\u7f6e\uff1f<\/h3>\n<p>A2: <code>selection<\/code> \u53c3\u6578\u61c9\u8a72\u7d81\u5b9a\u5230\u4e00\u500b <code>@State<\/code> \u5c6c\u6027\uff0c\u4ee5\u4fbf\u5728\u7528\u6236\u6539\u8b8a\u9078\u64c7\u6642\u66f4\u65b0\u72c0\u614b\u3002<\/p>\n<h3>Q3: \u5982\u679c\u9078\u64c7\u5668\u986f\u793a\u4e0d\u6b63\u78ba\uff0c\u6211\u8a72\u600e\u9ebc\u8fa6\uff1f<\/h3>\n<p>A3: \u6aa2\u67e5\u60a8\u7684 <code>ForEach<\/code> \u7bc4\u570d\u662f\u5426\u6b63\u78ba\uff0c\u4e26\u78ba\u4fdd <code>colors<\/code> \u9663\u5217\u5df2\u6b63\u78ba\u521d\u59cb\u5316\u3002<\/p>\n<p>---<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5728SwiftUI\u4e2d\uff0c\u9078\u64c7\u5668\u662f\u4e00\u7a2e\u53ef\u4ee5\u8b93\u60a8\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u63d0\u4f9b\u591a\u500b\u9078\u9805\u7684\u65b9\u6cd5\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728SwiftUI\u4e2d\u4f7f\u7528\u9078\u64c7\u5668\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u69cb\u5efa\u529f\u80fd\u5f37\u5927\u7684\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"author":1,"featured_media":7709,"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-7710","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\u9078\u64c7\u5668_1676961115.936333.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-20m","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7710","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=7710"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7710\/revisions"}],"predecessor-version":[{"id":13251,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7710\/revisions\/13251"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7709"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}