{"id":7734,"date":"2025-06-02T12:55:42","date_gmt":"2025-06-05T05:36:42","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7734"},"modified":"2025-06-05T12:55:42","modified_gmt":"2025-06-05T05:36:42","slug":"https-badgameshow-com-steven-510","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-510\/","title":{"rendered":"\u5728 SwiftUI \u4e2d\u5be6\u73fe\u5206\u9801\u8996\u5716\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, SwiftUI, \u5206\u9801\u8996\u5716, TabView, Picker\"><\/p>\n<p>SwiftUI \u662f Apple \u5728 WWDC 2019 \u63a8\u51fa\u7684\u65b0\u6846\u67b6\uff0c\u65e8\u5728\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u8de8\u5e73\u53f0\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u8a31\u591a\u958b\u767c\u8005\u5728\u4f7f\u7528 SwiftUI \u6642\uff0c\u7d93\u5e38\u9700\u8981\u5be6\u73fe\u5206\u9801\u8996\u5716\uff08TabView\uff09\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 SwiftUI \u4e2d\u5be6\u73fe\u5206\u9801\u8996\u5716\uff0c\u4e26\u63d0\u4f9b\u8a73\u7d30\u7684\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u548c\u5ef6\u4f38\u61c9\u7528\u7684\u5efa\u8b70\uff0c\u4ee5\u7b26\u54082025\u5e74\u7684\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5982\u4f55\u5728 SwiftUI \u4e2d\u4f7f\u7528\u5206\u9801\u8996\u5716\uff08TabView\uff09<\/h2>\n<p>\u5728 SwiftUI \u4e2d\uff0c\u5206\u9801\u8996\u5716\uff08TabView\uff09\u662f\u4e00\u500b\u975e\u5e38\u5e38\u898b\u4e14\u6613\u65bc\u4f7f\u7528\u7684\u529f\u80fd\u3002\u958b\u767c\u8005\u53ea\u9700\u5c07\u6240\u9700\u7684\u5167\u5bb9\u653e\u5165 <code>TabView<\/code> \u4e2d\uff0c\u4e26\u70ba\u6bcf\u500b\u5206\u9801\u6307\u5b9a\u6a19\u984c\u548c\u5716\u793a\uff0c\u5373\u53ef\u5b8c\u6210\u5206\u9801\u8996\u5716\u7684\u8a2d\u7f6e\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">TabView {\n    Text(\"\u7b2c\u4e00\u500b\u8996\u5716\")\n        .tabItem {\n            Image(systemName: \"1.circle\")\n            Text(\"\u7b2c\u4e00\")\n        }\n    Text(\"\u7b2c\u4e8c\u500b\u8996\u5716\")\n        .tabItem {\n            Image(systemName: \"2.circle\")\n            Text(\"\u7b2c\u4e8c\")\n        }\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u5efa\u7acb\u4e00\u500b\u5305\u542b\u5169\u500b\u5206\u9801\u7684\u5206\u9801\u8996\u5716\uff0c\u6bcf\u500b\u5206\u9801\u90fd\u6709\u81ea\u5df1\u7684\u6a19\u984c\u548c\u5716\u793a\u3002\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u8981\u66ff\u63db\u5716\u793a\u548c\u6a19\u984c\u3002<\/p>\n<h2>\u4f7f\u7528 Picker \u5be6\u73fe\u5206\u9801\u8996\u5716<\/h2>\n<p>\u9664\u4e86 <code>TabView<\/code>\uff0cSwiftUI \u4e5f\u652f\u6301\u4f7f\u7528 <code>Picker<\/code> \u4f86\u69cb\u5efa\u5206\u9801\u8996\u5716\u3002\u9019\u7a2e\u65b9\u5f0f\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u9748\u6d3b\u6027\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u5206\u9801\u8996\u5716\u7684\u5916\u89c0\u548c\u884c\u70ba\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">@State private var selection = 0\n\nVStack {\n    Picker(selection: $selection, label: Text(\"\")) {\n        Text(\"\u7b2c\u4e00\u500b\u8996\u5716\").tag(0)\n        Text(\"\u7b2c\u4e8c\u500b\u8996\u5716\").tag(1)\n    }\n    .pickerStyle(SegmentedPickerStyle())\n\n    if selection == 0 {\n        Text(\"\u7b2c\u4e00\u500b\u8996\u5716\")\n    } else {\n        Text(\"\u7b2c\u4e8c\u500b\u8996\u5716\")\n    }\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u4f7f\u7528 <code>Picker<\/code> \u5275\u5efa\u4e00\u500b\u5206\u9801\u8996\u5716\uff0c\u4e26\u4f7f\u7528 <code>pickerStyle<\/code> \u6307\u5b9a\u70ba\u5206\u6bb5\u5f0f\u9078\u64c7\u5668\uff08SegmentedPickerStyle\uff09\u3002\u9019\u6a23\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9078\u64c7\u7684\u6a19\u7c64\u986f\u793a\u4e0d\u540c\u7684\u8996\u5716\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u548c\u5e38\u898b\u554f\u984c<\/h2>\n<p>\u5728\u5be6\u4f5c\u5206\u9801\u8996\u5716\u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u554f\u984c\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><strong>\u8996\u5716\u4e0d\u986f\u793a\uff1a<\/strong>\u78ba\u4fdd\u5728 <code>TabView<\/code> \u6216 <code>Picker<\/code> \u4e2d\u6b63\u78ba\u8a2d\u7f6e\u5167\u5bb9\u3002<\/li>\n<li><strong>\u9078\u9805\u7121\u6cd5\u5207\u63db\uff1a<\/strong>\u6aa2\u67e5 <code>@State<\/code> \u5c6c\u6027\u662f\u5426\u6b63\u78ba\u7d81\u5b9a\u3002<\/li>\n<\/ul>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u5728 SwiftUI \u4e2d\uff0c\u4f7f\u7528\u5206\u9801\u8996\u5716\uff08TabView\uff09\u548c <code>Picker<\/code> \u662f\u5be6\u73fe\u5206\u9801\u5f0f\u61c9\u7528\u7a0b\u5e8f\u7684\u6709\u6548\u65b9\u6cd5\u3002\u958b\u767c\u8005\u53ea\u9700\u7c21\u55ae\u8a2d\u7f6e\uff0c\u5373\u53ef\u5feb\u901f\u5275\u5efa\u51fa\u9ad8\u6548\u7684\u5206\u9801\u8996\u5716\uff0c\u4e26\u63d0\u4f9b\u826f\u597d\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<ul>\n<li><strong>Q1\uff1a<\/strong>\u5982\u4f55\u81ea\u5b9a\u7fa9\u5206\u9801\u8996\u5716\u7684\u6a23\u5f0f\uff1f<br \/><strong>A\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684 <code>pickerStyle<\/code> \u4f86\u66f4\u6539 <code>Picker<\/code> \u7684\u5916\u89c0\uff0c\u6216\u81ea\u5b9a\u7fa9 <code>TabView<\/code> \u4e2d\u7684\u5167\u5bb9\u3002<\/li>\n<li><strong>Q2\uff1a<\/strong>\u5728 SwiftUI \u4e2d\u4f7f\u7528\u5206\u9801\u8996\u5716\u6642\uff0c\u5982\u4f55\u7ba1\u7406\u6578\u64da\u72c0\u614b\uff1f<br \/><strong>A\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528 <code>@State<\/code> \u6216 <code>@ObservedObject<\/code> \u4f86\u7ba1\u7406\u548c\u66f4\u65b0\u8996\u5716\u4e2d\u7684\u6578\u64da\u3002<\/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\u5206\u9801\u8996\u5716_1676962364.857149.webp\" alt=\"\u5728 SwiftUI \u4e2d\u4f7f\u7528\u5206\u9801\u8996\u5716\" title=\"SwiftUI, \u5206\u9801\u8996\u5716, \u7a0b\u5f0f\u78bc\u7bc4\u4f8b, \u529f\u80fd, \u9748\u6d3b\u6027\" width=\"1200\" height=\"628\"\/><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\u5206\u9801\u8996\u5716\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u591a\u500b\u8996\u5716\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u4e26\u4e14\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\u548c\u9748\u6d3b\u6027\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eSwiftUI\u4e2d\u7684\u5206\u9801\u8996\u5716\uff0c\u4e26\u63d0\u4f9b\u7c21\u55ae\u6613\u61c2\u7684\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u5176\u5be6\u4f5c\u5230\u81ea\u5df1\u7684\u5c08\u6848\u4e2d\u3002<\/p>\n","protected":false},"author":1,"featured_media":7733,"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-7734","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\u9801\u8996\u5716_1676962364.857149.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\/7734","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=7734"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7734\/revisions"}],"predecessor-version":[{"id":13112,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7734\/revisions\/13112"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7733"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}