{"id":7525,"date":"2025-05-30T14:06:04","date_gmt":"2025-06-05T07:06:03","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7525"},"modified":"2025-06-05T14:06:04","modified_gmt":"2025-06-05T07:06:03","slug":"https-badgameshow-com-steven-425-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-425-2\/","title":{"rendered":"\u30102025 \u6700\u65b0\u7248 Swift UIPickerView \u5b8c\u6574\u6559\u5b78\uff1a\u9078\u64c7\u5668\u63a7\u4ef6\u7684\u6700\u4f73\u5be6\u8e10\u3011"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UIPickerView, \u9078\u64c7\u5668\u63a7\u4ef6, iOS\u958b\u767c, Swift\u6559\u5b78\"><\/p>\n<h1>\u30102025 \u6700\u65b0\u7248 Swift UIPickerView \u5b8c\u6574\u6559\u5b78\uff1a\u9078\u64c7\u5668\u63a7\u4ef6\u7684\u6700\u4f73\u5be6\u8e10\u3011<\/h1>\n<p>UIPickerView \u662f iOS \u958b\u767c\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u9078\u64c7\u5668\u63a7\u4ef6\uff0c\u80fd\u5920\u8b93\u4f7f\u7528\u8005\u8f15\u9b06\u9078\u64c7\u591a\u500b\u9078\u9805\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 UIPickerView\uff0c\u5305\u62ec\u6700\u65b0\u7684\u8a9e\u6cd5\u3001\u5be6\u4f5c\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u53ca\u5ef6\u4f38\u61c9\u7528\uff0c\u5e6b\u52a9\u4f60\u638c\u63e1\u9019\u9805\u6280\u80fd\u3002<\/p>\n<h2>1. \u5efa\u7acb UIPickerView<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728\u8996\u5716\u4e2d\u5efa\u7acb\u4e00\u500b UIPickerView\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5be6\u73fe\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let pickerView = UIPickerView()\npickerView.translatesAutoresizingMaskIntoConstraints = false\nview.addSubview(pickerView)\n\n\/\/ \u8a2d\u5b9a Auto Layout \u7d04\u675f\nNSLayoutConstraint.activate([\n    pickerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),\n    pickerView.centerYAnchor.constraint(equalTo: view.centerYAnchor),\n    pickerView.widthAnchor.constraint(equalTo: view.widthAnchor),\n    pickerView.heightAnchor.constraint(equalToConstant: 200)\n])<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4e0d\u50c5\u5efa\u7acb\u4e86 UIPickerView\uff0c\u9084\u4f7f\u7528 Auto Layout \u5c07\u5176\u5c45\u4e2d\u986f\u793a\u5728\u8996\u5716\u4e2d\u3002<\/p>\n<h2>2. \u8a2d\u5b9a UIPickerView \u7684\u8cc7\u6599\u4f86\u6e90<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u70ba UIPickerView \u63d0\u4f9b\u8cc7\u6599\u4f86\u6e90\u3002\u9019\u88e1\u6211\u5011\u4f7f\u7528\u4e00\u500b\u5b57\u4e32\u9663\u5217\u4f5c\u70ba\u9078\u9805\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let dataSource = [\"\u9078\u9805 1\", \"\u9078\u9805 2\", \"\u9078\u9805 3\"]\npickerView.dataSource = self<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u8981\u8b93 UIPickerView \u80fd\u5920\u986f\u793a\u8cc7\u6599\uff0c\u6211\u5011\u5fc5\u9808\u4f7f\u7576\u524d\u8996\u5716\u63a7\u5236\u5668\u9075\u5faa <code>UIPickerViewDataSource<\/code> \u5354\u8b70\u3002<\/p>\n<h2>3. \u8a2d\u5b9a UIPickerView \u7684\u4ee3\u7406<\/h2>\n<p>\u70ba\u4e86\u8655\u7406\u4f7f\u7528\u8005\u7684\u9078\u64c7\uff0c\u6211\u5011\u9700\u8981\u8a2d\u7f6e\u4ee3\u7406\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">pickerView.delegate = self<\/code><\/pre>\n<p>\u540c\u6a23\uff0c\u9019\u9700\u8981\u7576\u524d\u8996\u5716\u63a7\u5236\u5668\u9075\u5faa <code>UIPickerViewDelegate<\/code> \u5354\u8b70\u3002<\/p>\n<h2>4. \u5be6\u73fe UIPickerView \u7684\u4ee3\u7406\u65b9\u6cd5<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5be6\u73fe\u4ee3\u7406\u65b9\u6cd5\u4f86\u8655\u7406\u9078\u9805\u7684\u9078\u64c7\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func numberOfComponents(in pickerView: UIPickerView) -> Int {\n    return 1 \/\/ \u8fd4\u56de\u5217\u6578\n}\n\nfunc pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {\n    return dataSource.count \/\/ \u8fd4\u56de\u9078\u9805\u6578\u91cf\n}\n\nfunc pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {\n    return dataSource[row] \/\/ \u8fd4\u56de\u5c0d\u61c9\u7684\u9078\u9805\n}\n\nfunc pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {\n    let selectedOption = dataSource[row]\n    print(\"\u9078\u64c7\u7684\u9078\u9805: \\(selectedOption)\") \/\/ \u8f38\u51fa\u9078\u64c7\u7684\u9078\u9805\n}<\/code><\/pre>\n<h2>5. \u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u4f60\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\u9047\u5230\u4efb\u4f55\u554f\u984c\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd\u4f60\u7684\u8996\u5716\u63a7\u5236\u5668\u9075\u5faa <code>UIPickerViewDataSource<\/code> \u548c <code>UIPickerViewDelegate<\/code> \u5354\u8b70\u3002<\/li>\n<li>\u6aa2\u67e5\u8cc7\u6599\u4f86\u6e90\u548c\u4ee3\u7406\u7684\u8a2d\u5b9a\u662f\u5426\u6b63\u78ba\u3002<\/li>\n<li>\u78ba\u4fdd\u8cc7\u6599\u4f86\u6e90\u6578\u64da\u4e0d\u70ba\u7a7a\uff0c\u5426\u5247 UIPickerView \u5c07\u4e0d\u6703\u986f\u793a\u4efb\u4f55\u9078\u9805\u3002<\/li>\n<\/ul>\n<h2>6. \u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4f60\u53ef\u4ee5\u5c07 UIPickerView \u8207\u5176\u4ed6 UI \u5143\u4ef6\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\u5728\u8868\u55ae\u4e2d\u9078\u64c7\u65e5\u671f\u6216\u6642\u9593\uff0c\u6216\u8005\u88fd\u4f5c\u81ea\u8a02\u7684\u9078\u64c7\u5668\u4ecb\u9762\u3002<\/p>\n<p>\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u638c\u63e1 Swift \u4e2d\u7684 UIPickerView \u4f7f\u7528\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/\ud83d\udcbbSwift-UIPickerView\u7528\u6cd5-\u9078\u64c7\u5668\u63a7\u4ef6-\ud83d\udcbb_1675477048.371039.webp\" alt=\"\ud83d\udcbbSwift UIPickerView\u7528\u6cd5 | \u9078\u64c7\u5668\u63a7\u4ef6 \ud83d\udcbb\" title=\"Swift UIPickerView, \u9078\u64c7\u5668\u63a7\u4ef6, UIPickerView\u5c6c\u6027, UIPickerView\u65b9\u6cd5, Swift\u4e2dUIPickerView\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: UIPickerView \u7684\u8cc7\u6599\u4f86\u6e90\u5fc5\u9808\u662f\u4ec0\u9ebc\u985e\u578b\uff1f<\/h3>\n<p>A1: UIPickerView \u7684\u8cc7\u6599\u4f86\u6e90\u5fc5\u9808\u662f\u9075\u5faa <code>UIPickerViewDataSource<\/code> \u5354\u8b70\u7684\u5c0d\u8c61\uff0c\u901a\u5e38\u662f\u4e00\u500b\u9663\u5217\u6216\u5176\u4ed6\u96c6\u5408\u985e\u578b\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u81ea\u8a02 UIPickerView \u7684\u5916\u89c0\uff1f<\/h3>\n<p>A2: \u4f60\u53ef\u4ee5\u4f7f\u7528 <code>pickerView(_:viewForRow:forComponent:reusing:)<\/code> \u65b9\u6cd5\u4f86\u81ea\u8a02\u6bcf\u4e00\u884c\u7684\u8996\u5716\uff0c\u5305\u62ec\u6587\u5b57\u984f\u8272\u3001\u5b57\u578b\u7b49\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):\u4e86\u89e3Swift\u4e2dUIPickerView\u7684\u7528\u6cd5\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528UIPickerView\u63a7\u4ef6\u4f86\u5275\u5efa\u9078\u64c7\u5668\uff0c\u4e26\u77ad\u89e3\u5982\u4f55\u8a2d\u7f6eUIPickerView\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u5728Swift\u4e2d\u4f7f\u7528UIPickerView\u3002<\/p>\n","protected":false},"author":1,"featured_media":7520,"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-7525","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\/\ud83d\udcbbSwift-UIPickerView\u7528\u6cd5-\u9078\u64c7\u5668\u63a7\u4ef6-\ud83d\udcbb_1675477048.371039.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Xn","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7525","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=7525"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7525\/revisions"}],"predecessor-version":[{"id":13390,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7525\/revisions\/13390"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7520"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}