{"id":7736,"date":"2025-05-30T13:11:53","date_gmt":"2025-06-05T05:42:52","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7736"},"modified":"2025-06-05T13:11:53","modified_gmt":"2025-06-05T05:42:52","slug":"https-badgameshow-com-steven-511","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-511\/","title":{"rendered":"\u5728 SwiftUI \u4e2d\u4f7f\u7528\u5206\u6bb5\u63a7\u5236\u5668\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u5be6\u4f5c\u7bc4\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, SwiftUI, \u5206\u6bb5\u63a7\u5236\u5668, \u6559\u5b78, \u5be6\u4f5c, 2025\"><\/p>\n<h2>\u4ec0\u9ebc\u662f\u5206\u6bb5\u63a7\u5236\u5668\uff1f<\/h2>\n<p>\u5206\u6bb5\u63a7\u5236\u5668\uff08Segmented Control\uff09\u662f\u4e00\u7a2e\u5e38\u898b\u7684 UI \u5143\u4ef6\uff0c\u5141\u8a31\u4f7f\u7528\u8005\u5728\u4e0d\u540c\u7684\u9078\u9805\u4e4b\u9593\u9032\u884c\u9078\u64c7\u3002\u9019\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5275\u5efa\u66f4\u76f4\u89c0\u7684\u61c9\u7528\u7a0b\u5f0f\u4ecb\u9762\uff0c\u63d0\u4f9b\u4f7f\u7528\u8005\u66f4\u591a\u7684\u4e92\u52d5\u9078\u64c7\u3002<\/p>\n<h2>\u5728 SwiftUI \u4e2d\u4f7f\u7528\u5206\u6bb5\u63a7\u5236\u5668<\/h2>\n<p>SwiftUI \u5728 2025 \u5e74\u5f15\u5165\u4e86\u4e00\u4e9b\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u4f7f\u5f97\u4f7f\u7528\u5206\u6bb5\u63a7\u5236\u5668\u8b8a\u5f97\u66f4\u7c21\u55ae\u3002\u4ee5\u4e0b\u662f\u5be6\u4f5c\u5206\u6bb5\u63a7\u5236\u5668\u7684\u5b8c\u6574\u6b65\u9a5f\u3002<\/p>\n<h3>\u6b65\u9a5f 1\uff1a\u5b9a\u7fa9\u5206\u6bb5\u63a7\u5236\u5668\u8b8a\u6578<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b9a\u7fa9\u4e00\u500b `@State` \u8b8a\u6578\u4f86\u8ffd\u8e2a\u9078\u64c7\u7684\u9805\u76ee\uff0c\u4e26\u5275\u5efa\u4e00\u500b `SegmentedControl` \u7684\u5be6\u4f8b\uff0c\u6307\u5b9a\u5176\u9078\u9805\u3002\u4ee5\u4e0b\u662f\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`swift<br \/>\n@State private var selection = 0<\/p>\n<p>var body: some View {<br \/>\n    SegmentedControl(selection: $selection) {<br \/>\n        Text(&#8220;Option 1&#8221;).tag(0)<br \/>\n        Text(&#8220;Option 2&#8221;).tag(1)<br \/>\n        Text(&#8220;Option 3&#8221;).tag(2)<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>\u6b65\u9a5f 2\uff1a\u5c07\u5206\u6bb5\u63a7\u5236\u5668\u52a0\u5165\u5230 View<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5c07\u5206\u6bb5\u63a7\u5236\u5668\u6dfb\u52a0\u5230\u60a8\u7684 SwiftUI \u8996\u5716\u4e2d\uff0c\u4e26\u6839\u64da\u4f7f\u7528\u8005\u7684\u9078\u64c7\u986f\u793a\u76f8\u61c9\u7684\u5167\u5bb9\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nvar body: some View {<br \/>\n    VStack {<br \/>\n        SegmentedControl(selection: $selection) {<br \/>\n            Text(&#8220;Option 1&#8221;).tag(0)<br \/>\n            Text(&#8220;Option 2&#8221;).tag(1)<br \/>\n            Text(&#8220;Option 3&#8221;).tag(2)<br \/>\n        }<br \/>\n        Text(&#8220;\u4f60\u9078\u64c7\u7684\u662f: \\(selection)&#8221;)<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>\u6b65\u9a5f 3\uff1a\u6839\u64da\u9078\u64c7\u57f7\u884c\u52d5\u4f5c<\/h3>\n<p>\u6839\u64da\u4f7f\u7528\u8005\u7684\u9078\u64c7\uff0c\u60a8\u53ef\u4ee5\u57f7\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u7bc4\uff0c\u5c55\u793a\u5982\u4f55\u6839\u64da\u9078\u64c7\u986f\u793a\u4e0d\u540c\u7684\u8996\u5716\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nvar body: some View {<br \/>\n    VStack {<br \/>\n        SegmentedControl(selection: $selection) {<br \/>\n            Text(&#8220;Option 1&#8221;).tag(0)<br \/>\n            Text(&#8220;Option 2&#8221;).tag(1)<br \/>\n            Text(&#8220;Option 3&#8221;).tag(2)<br \/>\n        }<\/p>\n<p>        if selection == 0 {<br \/>\n            Text(&#8220;\u9078\u64c7\u4e86 Option 1&#8221;)<br \/>\n        } else if selection == 1 {<br \/>\n            Text(&#8220;\u9078\u64c7\u4e86 Option 2&#8221;)<br \/>\n        } else {<br \/>\n            Text(&#8220;\u9078\u64c7\u4e86 Option 3&#8221;)<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u5e38\u898b\u932f\u8aa4\u6392\u9664<\/h2>\n<p>&#8211; **\u932f\u8aa4 1\uff1a\u9078\u64c7\u9805\u76ee\u4e0d\u66f4\u65b0**<br \/>\n\u78ba\u4fdd\u5728 `SegmentedControl` \u7684 `selection` \u7d81\u5b9a\u6b63\u78ba\u3002\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f `@State` \u8b8a\u6578\uff0c\u8acb\u78ba\u8a8d\u5b83\u5df2\u6b63\u78ba\u521d\u59cb\u5316\u3002<\/p>\n<p>&#8211; **\u932f\u8aa4 2\uff1aUI \u7121\u6cd5\u66f4\u65b0**<br \/>\n\u5982\u679c UI \u6c92\u6709\u96a8\u8457\u9078\u64c7\u8b8a\u5316\u800c\u66f4\u65b0\uff0c\u8acb\u6aa2\u67e5\u60a8\u7684 SwiftUI \u8996\u5716\u662f\u5426\u6b63\u78ba\u69cb\u5efa\uff0c\u4e26\u78ba\u4fdd\u72c0\u614b\u8b8a\u6578\uff08\u5982 `selection`\uff09\u7684\u6539\u8b8a\u80fd\u5920\u89f8\u767c\u8996\u5716\u66f4\u65b0\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5206\u6bb5\u63a7\u5236\u5668\u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u7c21\u55ae\u7684\u9078\u64c7\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u66f4\u8907\u96dc\u7684\u64cd\u4f5c\uff0c\u5982\u5207\u63db\u4e0d\u540c\u7684\u8996\u5716\u6216\u66f4\u65b0\u8cc7\u6599\u3002\u60a8\u53ef\u4ee5\u5c07\u5176\u8207\u5176\u4ed6 SwiftUI \u5143\u4ef6\u7d50\u5408\uff0c\u4f8b\u5982\u5716\u8868\u3001\u5217\u8868\u6216\u81ea\u5b9a\u7fa9\u8996\u5716\uff0c\u4ee5\u5275\u9020\u66f4\u8c50\u5bcc\u7684\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\u5206\u6bb5\u63a7\u5236\u5668_1676962452.836868.webp\" alt=\"\u5728 SwiftUI \u4e2d\u4f7f\u7528\u5206\u6bb5\u63a7\u5236\u5668\" title=\"SwiftUI, \u5206\u6bb5\u63a7\u5236\u5668, \u61c9\u7528\u7a0b\u5f0f, \u6548\u7387, \u6280\u8853\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u5728 SwiftUI \u4e2d\u8abf\u6574\u5206\u6bb5\u63a7\u5236\u5668\u7684\u6a23\u5f0f\uff1f<\/h3>\n<p>A1: \u60a8\u53ef\u4ee5\u4f7f\u7528 `.pickerStyle()` \u4fee\u98fe\u7b26\u4f86\u8abf\u6574\u5206\u6bb5\u63a7\u5236\u5668\u7684\u6a23\u5f0f\uff0c\u4f8b\u5982 `.segmented` \u4ee5\u4fbf\u4f7f\u5176\u770b\u8d77\u4f86\u66f4\u50cf\u50b3\u7d71\u7684\u5206\u6bb5\u63a7\u5236\u5668\u3002<\/p>\n<h3>Q2: \u5206\u6bb5\u63a7\u5236\u5668\u53ef\u4ee5\u8207\u5176\u4ed6 SwiftUI \u5143\u4ef6\u4e00\u8d77\u4f7f\u7528\u55ce\uff1f<\/h3>\n<p>A2: \u662f\u7684\uff0c\u5206\u6bb5\u63a7\u5236\u5668\u53ef\u4ee5\u8207\u5176\u4ed6\u5143\u4ef6\uff08\u5982\u5217\u8868\u3001\u5716\u8868\u7b49\uff09\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u5275\u5efa\u66f4\u8907\u96dc\u7684\u7528\u6236\u754c\u9762\u548c\u4ea4\u4e92\u3002<\/p>\n<h3>Q3: \u5206\u6bb5\u63a7\u5236\u5668\u662f\u5426\u53ef\u4ee5\u52d5\u614b\u751f\u6210\u9078\u9805\uff1f<\/h3>\n<p>A3: \u53ef\u4ee5\uff0c\u60a8\u53ef\u4ee5\u901a\u904e\u8ff4\u5708\u5c07\u9078\u9805\u52d5\u614b\u751f\u6210\u4e26\u50b3\u905e\u7d66 `SegmentedControl`\uff0c\u4f7f\u5176\u5177\u5099\u66f4\u5927\u7684\u9748\u6d3b\u6027\u548c\u53ef\u64f4\u5c55\u6027\u3002<\/p>\n<p>\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u60a8\u9806\u5229\u5728 SwiftUI \u4e2d\u4f7f\u7528\u5206\u6bb5\u63a7\u5236\u5668\uff01\u5982\u6709\u4efb\u4f55\u7591\u554f\uff0c\u6b61\u8fce\u5728\u4e0b\u65b9\u7559\u8a00\u4ea4\u6d41\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728SwiftUI\u4e2d\u4f7f\u7528\u5206\u6bb5\u63a7\u5236\u5668\uff0c\u8b93\u4f60\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u5efa\u7acb\u81ea\u5df1\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u4e26\u8b93\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u66f4\u52a0\u6709\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":7735,"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-7736","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\u5206\u6bb5\u63a7\u5236\u5668_1676962452.836868.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\/7736","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=7736"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7736\/revisions"}],"predecessor-version":[{"id":13101,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7736\/revisions\/13101"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7735"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}