{"id":7299,"date":"2025-05-29T12:52:20","date_gmt":"2025-06-05T04:59:19","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7299"},"modified":"2025-06-05T12:52:20","modified_gmt":"2025-06-05T04:59:19","slug":"https-badgameshow-com-steven-321","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-321\/","title":{"rendered":"Swift UIPickerView \u5b8c\u6574\u6307\u5357\uff1a\u8a2d\u5b9a\u8207\u4f7f\u7528\u6700\u4f73\u5be6\u8e10 (2025 \u6700\u65b0\u7248) \ud83c\udfa2"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UIPickerView, \u8a2d\u5b9a, \u4f7f\u7528\u65b9\u6cd5, iOS \u958b\u767c, Swift \u6559\u5b78\"><\/p>\n<h1>Swift UIPickerView \u5b8c\u6574\u6307\u5357\uff1a\u8a2d\u5b9a\u8207\u4f7f\u7528\u6700\u4f73\u5be6\u8e10 (2025 \u6700\u65b0\u7248) \ud83c\udfa2<\/h1>\n<p>UIPickerView \u662f iOS \u4e2d\u4e00\u500b\u6975\u70ba\u91cd\u8981\u7684\u5143\u4ef6\uff0c\u80fd\u5920\u8b93\u958b\u767c\u8005\u8f15\u9b06\u5730\u5efa\u7acb\u4e00\u500b\u8b93\u4f7f\u7528\u8005\u9078\u64c7\u7684\u9078\u55ae\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u8a2d\u5b9a UIPickerView\uff0c\u6db5\u84cb\u5f9e\u57fa\u672c\u64cd\u4f5c\u5230\u6700\u4f73\u5be6\u8e10\u7684\u8a73\u7d30\u6b65\u9a5f\u3002<\/p>\n<h2>\u5efa\u7acb UIPickerView<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u6211\u5011\u9700\u8981\u5728 Storyboard \u4e2d\u5efa\u7acb UIPickerView\uff0c\u4e26\u5c07\u5b83\u62d6\u653e\u5230 ViewController \u4e2d\u3002\u63a5\u8457\uff0c\u5c07 UIPickerView \u7684 delegate \u548c dataSource \u9023\u7d50\u5230 ViewController\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/Xh6yVXz.png\" alt=\"UIPickerView\" title=\"\u5728 Storyboard \u4e2d\u5efa\u7acb UIPickerView\" \/><\/p>\n<h2>\u5be6\u4f5c UIPickerViewDelegate \u548c UIPickerViewDataSource<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5728 ViewController \u4e2d\u5be6\u4f5c <code>UIPickerViewDelegate<\/code> \u548c <code>UIPickerViewDataSource<\/code> \u5354\u8b70\uff0c\u4e26\u5be6\u4f5c\u5176\u6240\u9700\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {\n    let items = [\"\u9078\u9805 1\", \"\u9078\u9805 2\", \"\u9078\u9805 3\"] \/\/ \u66ff\u63db\u70ba\u4f60\u7684\u9078\u9805\n\n    \/\/ MARK: - UIPickerViewDataSource\n    func numberOfComponents(in pickerView: UIPickerView) -> Int {\n        return 1 \/\/ \u8fd4\u56de pickerView \u7684\u5217\u6578\n    }\n\n    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {\n        return items.count \/\/ \u8fd4\u56de\u6bcf\u500b\u5217\u7684\u884c\u6578\n    }\n\n    \/\/ MARK: - UIPickerViewDelegate\n    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {\n        return items[row] \/\/ \u8fd4\u56de\u6bcf\u500b\u5217\u7684\u6bcf\u4e00\u884c\u7684\u6587\u5b57\n    }\n\n    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {\n        print(\"\u9078\u64c7\u4e86\uff1a\\(items[row])\") \/\/ \u9078\u64c7\u67d0\u4e00\u884c\u6642\u6703\u88ab\u547c\u53eb\n    }\n}\n<\/code><\/pre>\n<h2>\u8a2d\u5b9a UIPickerView<\/h2>\n<p>\u5728 ViewController \u7684 <code>viewDidLoad()<\/code> \u65b9\u6cd5\u4e2d\u8a2d\u5b9a UIPickerView\uff0c\u4ee5\u78ba\u4fdd\u5b83\u80fd\u5920\u6b63\u78ba\u5de5\u4f5c\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">override func viewDidLoad() {\n    super.viewDidLoad()\n    pickerView.delegate = self\n    pickerView.dataSource = self\n}\n<\/code><\/pre>\n<h2>\u5e38\u898b\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 UIPickerView \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff1a<\/p>\n<ul>\n<li><strong>\u672a\u8a2d\u7f6e delegate \u548c dataSource\uff1a<\/strong> \u78ba\u4fdd\u5728 <code>viewDidLoad()<\/code> \u65b9\u6cd5\u4e2d\u6b63\u78ba\u8a2d\u7f6e\u3002<\/li>\n<li><strong>\u6578\u64da\u6e90\u8fd4\u56de\u7684\u884c\u6578\u70ba 0\uff1a<\/strong> \u78ba\u4fdd\u4f60\u7684\u6578\u64da\u6e90\uff08\u5982 <code>items<\/code> \u9663\u5217\uff09\u5df2\u6b63\u78ba\u521d\u59cb\u5316\u3002<\/li>\n<li><strong>\u9078\u9805\u672a\u986f\u793a\uff1a<\/strong> \u78ba\u8a8d <code>titleForRow<\/code> \u65b9\u6cd5\u6b63\u78ba\u8fd4\u56de\u4e86\u5c0d\u61c9\u7684\u9078\u9805\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>UIPickerView \u4e0d\u50c5\u9650\u65bc\u986f\u793a\u975c\u614b\u9078\u9805\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u52d5\u614b\u6578\u64da\uff0c\u4f8b\u5982\u5f9e API \u7372\u53d6\u9078\u9805\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func fetchItems() {\n    \/\/ \u5047\u8a2d\u5f9e\u7db2\u7d61\u7372\u53d6\u6578\u64da\n    \/\/ \u66f4\u65b0 UI \u9700\u8981\u5728\u4e3b\u7dda\u7a0b\u4e0a\u57f7\u884c\n    DispatchQueue.main.async {\n        self.pickerView.reloadAllComponents()\n    }\n}\n<\/code><\/pre>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u8a2d\u5b9a\u548c\u4f7f\u7528 UIPickerView\uff0c\u5f9e\u5efa\u7acb UIPickerView \u5230\u5be6\u4f5c\u5fc5\u9700\u7684 delegate \u548c dataSource \u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u4e86\u5e38\u898b\u932f\u8aa4\u7684\u6392\u9664\u5efa\u8b70\u3002\u5e0c\u671b\u9019\u7bc7\u6307\u5357\u80fd\u5e6b\u52a9\u4f60\u5728 iOS \u958b\u767c\u4e2d\u66f4\u6709\u6548\u5730\u4f7f\u7528 UIPickerView\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UIPickerView \u53ef\u4ee5\u986f\u793a\u5716\u50cf\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u4f60\u53ef\u4ee5\u900f\u904e\u5be6\u4f5c <code>pickerView(_:viewForRow:forComponent:reusing:)<\/code> \u65b9\u6cd5\u4f86\u986f\u793a\u81ea\u5b9a\u7fa9\u7684\u8996\u5716\uff0c\u4f8b\u5982\u5716\u50cf\u3002<\/p>\n<h3>2. UIPickerView \u7684\u9078\u9805\u53ef\u4ee5\u52d5\u614b\u66f4\u65b0\u55ce\uff1f<\/h3>\n<p>\u53ef\u4ee5\uff0c\u900f\u904e\u8abf\u7528 <code>reloadAllComponents()<\/code> \u65b9\u6cd5\u4f86\u66f4\u65b0 UIPickerView \u7684\u9078\u9805\u3002<\/p>\n<h3>3. \u5982\u4f55\u5728\u9078\u64c7\u9805\u76ee\u6642\u57f7\u884c\u7279\u5b9a\u64cd\u4f5c\uff1f<\/h3>\n<p>\u5728 <code>pickerView(_:didSelectRow:inComponent:)<\/code> \u65b9\u6cd5\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6839\u64da\u9078\u64c7\u7684\u9805\u76ee\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\uff1a\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2dUIPickerView\u7684\u8a2d\u5b9a\u8207\u4f7f\u7528\u65b9\u6cd5\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5728iOSApp\u4e2d\u4f7f\u7528UIPickerView\u4f86\u9078\u64c7\u8cc7\u6599\u3002\u672c\u6587\u5c07\u6df1\u5165\u6dfa\u51fa\u5730\u4ecb\u7d39UIPickerView\u7684\u8a2d\u5b9a\u8207\u4f7f\u7528\u65b9\u6cd5\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5728iOSApp\u4e2d\u4f7f\u7528UIPickerView\u4f86\u9078\u64c7\u8cc7\u6599\u3002<\/p>\n","protected":false},"author":1,"featured_media":7298,"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-7299","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-\u8a2d\u5b9a\u53ca\u4f7f\u7528\u65b9\u6cd5-\ud83c\udfa2_1675410293.683027.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1TJ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7299","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=7299"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7299\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7298"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}