{"id":6788,"date":"2025-06-02T12:55:25","date_gmt":"2025-06-05T05:26:24","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6788"},"modified":"2025-06-05T12:55:25","modified_gmt":"2025-06-05T05:26:24","slug":"https-badgameshow-com-steven-70","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-70\/","title":{"rendered":"Swift UIPickerView \u4f7f\u7528\u6307\u5357 2025 &#8211; \u73fe\u4ee3\u5316\u6efe\u8f2a\u9078\u64c7\u5668\u6559\u5b78"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UIPickerView, \u6efe\u8f2a\u9078\u64c7\u5668, \u6559\u5b78, 2025, iOS \u958b\u767c\"><\/p>\n<h2>\u4ec0\u9ebc\u662f UIPickerView\uff1f<\/h2>\n<p>UIPickerView \u662f iOS \u61c9\u7528\u7a0b\u5f0f\u4e2d\u4e00\u500b\u975e\u5e38\u91cd\u8981\u4e14\u5e38\u898b\u7684 UI \u5143\u4ef6\u3002\u5b83\u5141\u8a31\u4f7f\u7528\u8005\u5f9e\u4e00\u500b\u6efe\u52d5\u7684\u5217\u8868\u4e2d\u9078\u64c7\u4e00\u500b\u9078\u9805\uff0c\u9019\u4f7f\u5f97\u7528\u6236\u754c\u9762\u66f4\u52a0\u53cb\u597d\u548c\u76f4\u89c0\u3002\u7121\u8ad6\u662f\u5728\u9078\u64c7\u65e5\u671f\u3001\u6642\u9593\u9084\u662f\u5176\u4ed6\u9078\u9805\uff0cUIPickerView \u90fd\u80fd\u5920\u63d0\u4f9b\u826f\u597d\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 UIPickerView<\/h2>\n<p>\u4f7f\u7528 UIPickerView \u7684\u7b2c\u4e00\u6b65\u662f\u5c07\u5b83\u6dfb\u52a0\u5230\u4f60\u7684 View Controller\u3002\u9019\u53ef\u4ee5\u901a\u904e Storyboard \u62d6\u653e\u6216\u662f\u7a0b\u5f0f\u78bc\u4e2d\u521d\u59cb\u5316\u4f86\u5be6\u73fe\u3002<\/p>\n<h3>\u6b65\u9a5f 1: \u6dfb\u52a0 UIPickerView<\/h3>\n<pre><code class=\"language-swift\">\nlet pickerView = UIPickerView()\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 2: \u8a2d\u5b9a Delegate \u548c DataSource<\/h3>\n<p>\u4f60\u9700\u8981\u5ba3\u544a\u4e00\u500b\u7b26\u5408 <code>UIPickerViewDelegate<\/code> \u548c <code>UIPickerViewDataSource<\/code> \u5354\u5b9a\u7684\u7269\u4ef6\uff0c\u4e26\u5c07\u5b83\u5011\u8a2d\u5b9a\u7d66 <code>UIPickerView<\/code>\u3002\u9019\u6a23 UIPickerView \u624d\u80fd\u77e5\u9053\u8981\u986f\u793a\u4ec0\u9ebc\u8cc7\u6599\u4ee5\u53ca\u4f7f\u7528\u8005\u9078\u64c7\u4e86\u4ec0\u9ebc\u3002<\/p>\n<pre><code class=\"language-swift\">\nclass ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {\n    @IBOutlet weak var pickerView: UIPickerView!\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        pickerView.delegate = self\n        pickerView.dataSource = self\n    }\n}\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 3: \u5be6\u4f5c DataSource \u548c Delegate \u65b9\u6cd5<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u4f60\u9700\u8981\u5be6\u4f5c <code>UIPickerViewDataSource<\/code> \u548c <code>UIPickerViewDelegate<\/code> \u7684\u5fc5\u8981\u65b9\u6cd5\uff0c\u4ee5\u63d0\u4f9b\u986f\u793a\u5167\u5bb9\u548c\u8655\u7406\u9078\u64c7\u4e8b\u4ef6\u3002<\/p>\n<pre><code class=\"language-swift\">\n\/\/ MARK: - UIPickerViewDataSource\nfunc numberOfComponents(in pickerView: UIPickerView) -> Int {\n    return 1\n}\n\nfunc pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {\n    return 10 \/\/ \u53ef\u6839\u64da\u5be6\u969b\u9700\u6c42\u8fd4\u56de\u6578\u91cf\n}\n\n\/\/ MARK: - UIPickerViewDelegate\nfunc pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {\n    return \"\u9078\u9805 \\(row + 1)\" \/\/ \u8fd4\u56de\u9078\u9805\u6a19\u984c\n}\n\nfunc pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {\n    print(\"\u9078\u64c7\u7684\u884c: \\(row + 1)\") \/\/ \u8655\u7406\u9078\u64c7\u4e8b\u4ef6\n}\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 4: \u81ea\u8a02 UIPickerView<\/h3>\n<p>\u6700\u5f8c\uff0c\u4f60\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u81ea\u8a02 UIPickerView \u7684\u5c6c\u6027\uff0c\u4f8b\u5982\u80cc\u666f\u984f\u8272\u3001\u6587\u5b57\u984f\u8272\u7b49\uff0c\u8b93\u5b83\u66f4\u7b26\u5408\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u98a8\u683c\u3002<\/p>\n<pre><code class=\"language-swift\">\npickerView.backgroundColor = UIColor.white\npickerView.tintColor = UIColor.blue\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<ul>\n<li><strong>\u672a\u986f\u793a\u9078\u9805\uff1a<\/strong>\u78ba\u4fdd\u4f60\u5df2\u6b63\u78ba\u8a2d\u7f6e delegate \u548c data source\u3002<\/li>\n<li><strong>\u9078\u64c7\u4e8b\u4ef6\u7121\u6cd5\u89f8\u767c\uff1a<\/strong>\u6aa2\u67e5\u662f\u5426\u6b63\u78ba\u5be6\u4f5c\u4e86 <code>didSelectRow<\/code> \u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>UIPickerView \u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u57fa\u672c\u7684\u9078\u64c7\u529f\u80fd\uff0c\u4f60\u9084\u53ef\u4ee5\u7d50\u5408\u5176\u4ed6 UI \u5143\u4ef6\uff0c\u4f8b\u5982 UITextField\uff0c\u4f86\u5275\u5efa\u66f4\u8907\u96dc\u7684\u7528\u6236\u754c\u9762\uff0c\u6216\u662f\u5c07\u9078\u64c7\u7684\u7d50\u679c\u50b3\u905e\u5230\u5176\u4ed6\u8996\u5716\u63a7\u5236\u5668\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>UIPickerView \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u4e14\u9748\u6d3b\u7684 UI \u5143\u4ef6\uff0c\u80fd\u5920\u70ba iOS \u61c9\u7528\u7a0b\u5f0f\u63d0\u4f9b\u826f\u597d\u7684\u7528\u6236\u9ad4\u9a57\u3002\u900f\u904e\u9069\u7576\u7684\u8a2d\u7f6e\u548c\u81ea\u8a02\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u5176\u6574\u5408\u5230\u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u63d0\u5347\u6574\u9ad4\u7684 UI \u6548\u679c\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: UIPickerView \u5982\u4f55\u8207 UITextField \u6574\u5408\uff1f<\/h3>\n<p>A1: \u4f60\u53ef\u4ee5\u5c07 UIPickerView \u8a2d\u7f6e\u70ba UITextField \u7684\u8f38\u5165\u8996\u5716\uff0c\u8b93\u4f7f\u7528\u8005\u5728\u9ede\u64ca\u6587\u672c\u6846\u6642\u5f48\u51fa\u9078\u64c7\u5668\u3002<\/p>\n<h3>Q2: \u662f\u5426\u53ef\u4ee5\u81ea\u8a02 UIPickerView \u7684\u6a23\u5f0f\uff1f<\/h3>\n<p>A2: \u662f\u7684\uff0c\u4f60\u53ef\u4ee5\u900f\u904e\u8a2d\u5b9a\u5c6c\u6027\u4f86\u6539\u8b8a UIPickerView \u7684\u5916\u89c0\uff0c\u4f8b\u5982\u6539\u8b8a\u80cc\u666f\u984f\u8272\u548c\u9078\u9805\u6587\u5b57\u984f\u8272\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u8655\u7406 UIPickerView \u7684\u591a\u500b\u7d44\u4ef6\uff1f<\/h3>\n<p>A3: \u5728 <code>numberOfComponents<\/code> \u65b9\u6cd5\u4e2d\u8fd4\u56de\u7d44\u4ef6\u6578\u91cf\uff0c\u4e26\u5728 <code>numberOfRowsInComponent<\/code> \u4e2d\u70ba\u6bcf\u500b\u7d44\u4ef6\u63d0\u4f9b\u9078\u9805\u6578\u91cf\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UIPickerView\u4f7f\u7528\ud83c\udfa8-\u6efe\u8f2a\u9078\u64c7\u5668\u6559\u5b78_1675395792.266389.webp\" alt=\"Swift UIPickerView \u4f7f\u7528\u6307\u5357\" title=\"Swift UIPickerView\u3001\u6efe\u8f2a\u9078\u64c7\u5668\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\" width=\"1200\" height=\"628\"\/><br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684UIPickerView\u6efe\u8f2a\u9078\u64c7\u5668\uff0c\u4e86\u89e3\u5982\u4f55\u8a2d\u7f6eUIPickerView\u7684\u5c6c\u6027\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528UIPickerView\u7684Delegate\u548cDataSource\u65b9\u6cd5\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u5728Swift\u4e2d\u4f7f\u7528UIPickerView\u6efe\u8f2a\u9078\u64c7\u5668\u3002<\/p>\n","protected":false},"author":1,"featured_media":6787,"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-6788","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\u4f7f\u7528\ud83c\udfa8-\u6efe\u8f2a\u9078\u64c7\u5668\u6559\u5b78_1675395792.266389.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Lu","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6788","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=6788"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6788\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6787"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}