{"id":7408,"date":"2025-06-04T14:12:03","date_gmt":"2025-06-05T06:29:03","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7408"},"modified":"2025-06-05T14:12:03","modified_gmt":"2025-06-05T06:29:03","slug":"https-badgameshow-com-steven-375","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-375\/","title":{"rendered":"Swift UIPickerView 2025 \u6700\u65b0\u6559\u5b78\uff1a\u5efa\u7acb\u4e92\u52d5\u5f0f\u9078\u64c7\u5668\u7684\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UIPickerView, \u9078\u64c7\u5668, iOS\u61c9\u7528\u958b\u767c, Swift 2025\"><\/p>\n<h2>\u4ecb\u7d39<\/h2>\n<p>Swift UIPickerView\u662f\u4e00\u500b\u6975\u5176\u6709\u7528\u7684\u5143\u4ef6\uff0c\u80fd\u5920\u8b93\u4f60\u5728iOS\u61c9\u7528\u7a0b\u5f0f\u4e2d\u5feb\u901f\u5efa\u7acb\u4e00\u500b\u4e92\u52d5\u5f0f\u7684\u9078\u64c7\u4ecb\u9762\u3002\u7121\u8ad6\u662f\u9078\u64c7\u65e5\u671f\u3001\u6642\u9593\u9084\u662f\u5730\u9ede\uff0cUIPickerView\u90fd\u80fd\u63d0\u4f9b\u6d41\u66a2\u7684\u4f7f\u7528\u9ad4\u9a57\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Swift\u4e2d\u4f7f\u7528UIPickerView\uff0c\u4e26\u63d0\u4f9b2025\u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5efa\u7acbUIPickerView\u9078\u64c7\u5668<\/h2>\n<p>\u8981\u4f7f\u7528UIPickerView\u9078\u64c7\u5668\uff0c\u4f60\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u5efa\u7acb\u4e00\u500bUIPickerView\u7269\u4ef6\u3002<\/li>\n<li>\u5c07UIPickerView\u6dfb\u52a0\u5230\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u754c\u9762\u4e2d\u3002<\/li>\n<li>\u5be6\u4f5cUIPickerViewDataSource\u548cUIPickerViewDelegate\u5354\u5b9a\u3002<\/li>\n<\/ol>\n<h3>\u6b65\u9a5f1\uff1a\u5efa\u7acbUIPickerView\u7269\u4ef6<\/h3>\n<p>\u9996\u5148\uff0c\u5728\u4f60\u7684ViewController\u4e2d\uff0c\u5efa\u7acb\u4e00\u500bUIPickerView\u7269\u4ef6\u4e26\u8a2d\u7f6e\u5176\u6846\u67b6\uff1a<\/p>\n<pre><code>let pickerView = UIPickerView()\npickerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 200)\nview.addSubview(pickerView)<\/code><\/pre>\n<h3>\u6b65\u9a5f2\uff1a\u5be6\u4f5cUIPickerViewDataSource<\/h3>\n<p>\u63a5\u8457\uff0c\u4f60\u9700\u8981\u5be6\u4f5cUIPickerViewDataSource\u5354\u5b9a\u4e2d\u7684\u5169\u500b\u6838\u5fc3\u65b9\u6cd5\uff1a<\/p>\n<pre><code>extension ViewController: UIPickerViewDataSource {\n    func numberOfComponents(in pickerView: UIPickerView) -> Int {\n        return 1 \/\/ \u6307\u5b9a\u5217\u6578\n    }\n    \n    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {\n        return options.count \/\/ \u8fd4\u56de\u884c\u6578\n    }\n}<\/code><\/pre>\n<h3>\u6b65\u9a5f3\uff1a\u5be6\u4f5cUIPickerViewDelegate<\/h3>\n<p>\u7136\u5f8c\uff0c\u5be6\u4f5cUIPickerViewDelegate\u5354\u5b9a\u4f86\u8655\u7406\u9078\u64c7\u7684\u884c\uff1a<\/p>\n<pre><code>extension ViewController: UIPickerViewDelegate {\n    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {\n        return options[row] \/\/ \u8fd4\u56de\u6bcf\u884c\u7684\u6587\u5b57\n    }\n    \n    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {\n        print(\"\u9078\u64c7\u4e86: \\(options[row])\") \/\/ \u8655\u7406\u4f7f\u7528\u8005\u7684\u9078\u64c7\n    }\n}<\/code><\/pre>\n<h3>\u8a2d\u7f6eDataSource\u8207Delegate<\/h3>\n<p>\u6700\u5f8c\uff0c\u5c07UIPickerView\u7684dataSource\u548cdelegate\u8a2d\u7f6e\u70ba\u7576\u524d\u7684ViewController\uff1a<\/p>\n<pre><code>pickerView.dataSource = self\npickerView.delegate = self<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679cUIPickerView\u6c92\u6709\u6b63\u78ba\u986f\u793a\u6578\u64da\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd\u4f60\u7684dataSource\u548cdelegate\u5df2\u6b63\u78ba\u8a2d\u7f6e\u3002<\/li>\n<li>\u6aa2\u67e5\u63d0\u4f9b\u7684\u6578\u64da\u6e90\uff08\u5982options\u9663\u5217\uff09\u662f\u5426\u4e0d\u70ba\u7a7a\u3002<\/li>\n<li>\u78ba\u8a8d\u4f60\u7684UIPickerView\u7269\u4ef6\u5df2\u6dfb\u52a0\u81f3\u8996\u5716\u4e2d\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>UIPickerView\u53ef\u4ee5\u8207\u5176\u4ed6UI\u5143\u4ef6\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982UITextField\uff0c\u4f86\u5275\u5efa\u66f4\u8c50\u5bcc\u7684\u7528\u6236\u4e92\u52d5\u9ad4\u9a57\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Swift UIPickerView\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u4f60\u5728iOS\u61c9\u7528\u7a0b\u5f0f\u4e2d\u5feb\u901f\u69cb\u5efa\u9078\u64c7\u4ecb\u9762\u3002\u900f\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u4f60\u53ef\u4ee5\u8f15\u9b06\u5be6\u4f5c\u4e00\u500b\u529f\u80fd\u5b8c\u5584\u7684\u9078\u64c7\u5668\uff0c\u4e26\u63d0\u4f9b\u826f\u597d\u7684\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UIPickerView\u53ef\u4ee5\u7528\u65bc\u9078\u64c7\u54ea\u4e9b\u985e\u578b\u7684\u6578\u64da\uff1f<\/h3>\n<p>UIPickerView\u53ef\u4ee5\u7528\u65bc\u9078\u64c7\u591a\u7a2e\u6578\u64da\uff0c\u4f8b\u5982\u65e5\u671f\u3001\u6642\u9593\u3001\u5730\u9ede\u3001\u984f\u8272\u7b49\uff0c\u751a\u81f3\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u7fa9\u7684\u6578\u64da\u985e\u578b\u3002<\/p>\n<h3>2. \u5982\u4f55\u5728UIPickerView\u4e2d\u986f\u793a\u5716\u7247\uff1f<\/h3>\n<p>\u8981\u5728UIPickerView\u4e2d\u986f\u793a\u5716\u7247\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528pickerView:viewForRow:forComponent:\u65b9\u6cd5\u4f86\u8fd4\u56de\u4e00\u500b\u5305\u542bUIImageView\u7684\u81ea\u5b9a\u7fa9\u8996\u5716\u3002<\/p>\n<h3>3. UIPickerView\u662f\u5426\u652f\u6301\u591a\u5217\u9078\u64c7\uff1f<\/h3>\n<p>\u662f\u7684\uff0cUIPickerView\u53ef\u4ee5\u652f\u6301\u591a\u5217\u9078\u64c7\uff0c\u53ea\u9700\u5728numberOfComponents\u65b9\u6cd5\u4e2d\u8fd4\u56de\u591a\u65bc1\u7684\u503c\uff0c\u4e26\u5728\u76f8\u61c9\u7684\u65b9\u6cd5\u4e2d\u63d0\u4f9b\u6bcf\u5217\u7684\u6578\u64da\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UIPickerView\u9078\u64c7\u5668_1675475442.371573.webp\" alt=\"Swift UIPickerView\u9078\u64c7\u5668\" title=\"Swift UIPickerView\u9078\u64c7\u5668\u7684\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728iOS\u61c9\u7528\u4e2d\u4f7f\u7528\u9078\u64c7\u5668\u3002\" width=\"1200\" height=\"628\"\/><br \/>\n&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):\u5728Swift\u4e2d\uff0cUIPickerView\u9078\u64c7\u5668\u662f\u4e00\u7a2e\u53ef\u4ee5\u8b93\u4f7f\u7528\u8005\u5f9e\u4e00\u7cfb\u5217\u9078\u9805\u4e2d\u9078\u64c7\u7684\u5de5\u5177\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528UIPickerView\u9078\u64c7\u5668\uff0c\u4ee5\u53ca\u5982\u4f55\u5728Swift\u4e2d\u5be6\u73fe\u5b83\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":7407,"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-7408","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\u9078\u64c7\u5668_1675475442.371573.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Vu","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7408","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=7408"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7408\/revisions"}],"predecessor-version":[{"id":13548,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7408\/revisions\/13548"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7407"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}