{"id":7074,"date":"2025-06-03T15:11:58","date_gmt":"2025-06-05T07:18:58","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7074"},"modified":"2025-06-05T15:11:58","modified_gmt":"2025-06-05T07:18:58","slug":"https-badgameshow-com-steven-212","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-212\/","title":{"rendered":"Swift UIPickerView \ud83c\udfb2 \u5b8c\u6574\u6559\u5b78\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UIPickerView, \u9078\u64c7\u6efe\u8f2a, iOS, Swift \u6559\u5b78\"><\/p>\n<h1>Swift UIPickerView \ud83c\udfb2 \u5b8c\u6574\u6559\u5b78\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5ee3\u53d7\u6b61\u8fce\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u9ad8\u6548\u5730\u958b\u767c\u529f\u80fd\u5f37\u5927\u7684 iOS \u61c9\u7528\u7a0b\u5f0f\u3002\u5728 Swift \u4e2d\uff0c\u900f\u904e\u4f7f\u7528 <strong>UIPickerView<\/strong>\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5275\u5efa\u9078\u64c7\u6efe\u8f2a\uff0c\u4f7f\u7528\u8005\u80fd\u5920\u5f9e\u591a\u500b\u9078\u9805\u4e2d\u9032\u884c\u9078\u64c7\u3002<\/p>\n<h2>UIPickerView \u7684\u57fa\u672c\u4ecb\u7d39<\/h2>\n<p><strong>UIPickerView<\/strong> \u662f\u4e00\u500b\u5f37\u5927\u7684 UI \u5143\u7d20\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5275\u5efa\u53ef\u4f9b\u4f7f\u7528\u8005\u9078\u64c7\u7684\u6efe\u8f2a\u3002\u900f\u904e\u7c21\u55ae\u7684\u914d\u7f6e\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u6efe\u8f2a\u7684\u6a23\u5f0f\u53ca\u5167\u5bb9\uff0c\u63d0\u4f9b\u6d41\u66a2\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2>\u5275\u5efa UIPickerView \u7684\u6b65\u9a5f<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 <strong>UIPickerView<\/strong> \u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3>1. \u5275\u5efa UIPickerView \u5be6\u4f8b<\/h3>\n<pre><code class=\"language-swift line-numbers\">let pickerView = UIPickerView()\npickerView.delegate = self\npickerView.dataSource = self\n<\/code><\/pre>\n<h3>2. \u5be6\u4f5c UIPickerViewDelegate \u548c UIPickerViewDataSource<\/h3>\n<p>\u958b\u767c\u8005\u9700\u8981\u5be6\u4f5c\u4ee5\u4e0b\u5354\u5b9a\uff0c\u4ee5\u8a2d\u5b9a\u6efe\u8f2a\u4e2d\u7684\u9078\u9805\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">extension YourViewController: UIPickerViewDelegate, UIPickerViewDataSource {\n    func numberOfComponents(in pickerView: UIPickerView) -> Int {\n        return 1\n    }\n\n    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {\n        return options.count\n    }\n\n    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {\n        return options[row]\n    }\n}\n<\/code><\/pre>\n<h3>3. \u9810\u8a2d\u9078\u64c7\u7684\u9078\u9805<\/h3>\n<p>\u4f7f\u7528 <strong>selectRow<\/strong> \u65b9\u6cd5\u4f86\u8a2d\u5b9a\u9810\u8a2d\u7684\u9078\u64c7\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">pickerView.selectRow(0, inComponent: 0, animated: false)<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u78ba\u4fdd delegate \u548c dataSource \u88ab\u6b63\u78ba\u8a2d\u5b9a\uff1a<\/strong>\u5982\u679c\u6efe\u8f2a\u4e0d\u986f\u793a\u4efb\u4f55\u9078\u9805\uff0c\u6aa2\u67e5\u9019\u5169\u500b\u5c6c\u6027\u662f\u5426\u6b63\u78ba\u6307\u5411\u76f8\u61c9\u7684\u7269\u4ef6\u3002<\/li>\n<li><strong>\u9078\u9805\u6578\u91cf\u4e0d\u6b63\u78ba\uff1a<\/strong>\u5728 <code>numberOfRowsInComponent<\/code> \u65b9\u6cd5\u4e2d\u8fd4\u56de\u7684\u6578\u5b57\u5fc5\u9808\u8207 <code>options<\/code> \u9663\u5217\u7684\u6578\u91cf\u76f8\u7b26\u3002<\/li>\n<li><strong>UI \u66f4\u65b0\u4e0d\u53ca\u6642\uff1a<\/strong>\u5982\u9700\u5728\u8cc7\u6599\u8b8a\u66f4\u5f8c\u66f4\u65b0 UIPickerView\uff0c\u8acb\u4f7f\u7528 <code>reloadAllComponents()<\/code> \u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u9078\u64c7\u529f\u80fd\uff0c<strong>UIPickerView<\/strong> \u9084\u53ef\u4ee5\u7528\u65bc\u65e5\u671f\u9078\u64c7\u3001\u6642\u9593\u9078\u64c7\u7b49\u5834\u666f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9032\u884c\u64f4\u5c55\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UIPickerView-\ud83c\udfb2\u9078\u64c7\u6efe\u8f2a\uff01_1675401971.722133.webp\" alt=\"Swift UIPickerView \ud83c\udfb2\u9078\u64c7\u6efe\u8f2a\uff01\" title=\"Swift UIPickerView, \u9078\u64c7\u6efe\u8f2a, iOS, UI \u5143\u7d20, Swift\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UIPickerView \u53ef\u4ee5\u81ea\u5b9a\u7fa9\u6a23\u5f0f\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>pickerView(_:viewForRow:forComponent:reusing:)<\/code> \u65b9\u6cd5\u4f86\u81ea\u5b9a\u7fa9\u6bcf\u4e00\u884c\u7684\u6a23\u5f0f\u3002<\/p>\n<h3>2. UIPickerView \u5982\u4f55\u8207\u5176\u4ed6 UI \u5143\u7d20\u4e92\u52d5\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5c07 UIPickerView \u8207 UIButton \u6216\u5176\u4ed6 UI \u5143\u7d20\u7d50\u5408\uff0c\u5c07\u9078\u64c7\u7684\u7d50\u679c\u76f4\u63a5\u986f\u793a\u5728\u9019\u4e9b\u5143\u7d20\u4e2d\u3002<\/p>\n<h3>3. \u5982\u4f55\u8655\u7406 UIPickerView \u7684\u9078\u64c7\u4e8b\u4ef6\uff1f<\/h3>\n<p>\u53ef\u4ee5\u5be6\u4f5c <code>pickerView(_:didSelectRow:inComponent:)<\/code> \u65b9\u6cd5\uff0c\u4ee5\u4fbf\u5728\u4f7f\u7528\u8005\u9078\u64c7\u67d0\u4e00\u9078\u9805\u6642\u57f7\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u4e86\u89e3\u5982\u4f55\u4f7f\u7528SwiftUIPickerView\u9078\u64c7\u6efe\u8f2a\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u5728iOS\u4e0a\u5efa\u7acb\u9078\u64c7\u6efe\u8f2a\uff0c\u4e26\u5c07\u5176\u8207\u5176\u4ed6UI\u5143\u7d20\u7d50\u5408\u4f7f\u7528\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528SwiftUIPickerView\u9078\u64c7\u6efe\u8f2a\uff0c\u4ee5\u53ca\u5982\u4f55\u5c07\u5176\u8207\u5176\u4ed6UI\u5143\u7d20\u7d50\u5408\u4f7f\u7528\u3002<\/p>\n","protected":false},"author":1,"featured_media":7073,"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-7074","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\/Swift-UIPickerView-\ud83c\udfb2\u9078\u64c7\u6efe\u8f2a\uff01_1675401971.722133.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Q6","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7074","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=7074"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7074\/revisions"}],"predecessor-version":[{"id":13396,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7074\/revisions\/13396"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7073"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}