{"id":6794,"date":"2025-06-02T12:57:49","date_gmt":"2025-06-05T05:26:48","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6794"},"modified":"2025-06-05T12:57:49","modified_gmt":"2025-06-05T05:26:48","slug":"https-badgameshow-com-steven-73","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-73\/","title":{"rendered":"2025 \u5e74\u6700\u65b0 Swift \u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u6559\u5b78 \ud83c\udfa8\ud83d\udd25"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u81ea\u5b9a\u7fa9\u9078\u64c7\u5668, Swift \u7a0b\u5f0f\u6559\u5b78, iOS \u958b\u767c\"><\/p>\n<h2>\u4ec0\u9ebc\u662f Swift \u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\uff1f<\/h2>\n<p>Swift \u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u662f\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8b93\u4f60\u70ba\u61c9\u7528\u7a0b\u5f0f\u5275\u5efa\u7368\u7279\u7684\u7528\u6236\u4ecb\u9762\u3002\u900f\u904e\u81ea\u5b9a\u7fa9\u7684\u9078\u64c7\u5668\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8abf\u6574\u6309\u9215\u3001\u6587\u5b57\u3001\u5716\u50cf\u53ca\u5176\u4ed6\u4ecb\u9762\u5143\u7d20\u7684\u5916\u89c0\u548c\u884c\u70ba\uff0c\u4f7f\u5f97\u61c9\u7528\u7a0b\u5f0f\u5728\u8996\u89ba\u548c\u529f\u80fd\u4e0a\u90fd\u66f4\u5177\u5438\u5f15\u529b\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 Swift \u81ea\u5b9a\u7fa9\u9078\u64c7\u5668<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 Swift \u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u7684\u57fa\u672c\u6d41\u7a0b\uff0c\u5305\u542b\u5fc5\u8981\u7684\u7a0b\u5f0f\u78bc\u793a\u7bc4\u3002<\/p>\n<h3>\u6b65\u9a5f 1: \u5275\u5efa ViewController<\/h3>\n<p>\u5728\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u9996\u5148\u9700\u8981\u5275\u5efa\u4e00\u500b\u65b0\u7684 ViewController\uff0c\u4e26\u5c07\u5176\u8a2d\u7f6e\u70ba\u4e3b\u8981\u7684 ViewController\u3002<\/p>\n<h3>\u6b65\u9a5f 2: \u6dfb\u52a0 UIPickerView<\/h3>\n<p>\u5728 ViewController \u4e2d\u6dfb\u52a0\u4e00\u500b <code>UIPickerView<\/code>\uff0c\u9019\u5c07\u4f5c\u70ba\u4f60\u7684\u9078\u64c7\u5668\u3002<\/p>\n<h3>\u6b65\u9a5f 3: \u8a2d\u7f6e Delegate \u548c DataSource<\/h3>\n<p>\u4f7f\u7528 Swift \u8a9e\u8a00\u4f86\u5275\u5efa\u81ea\u5b9a\u7fa9\u7684 <code>UIPickerViewDelegate<\/code> \u548c <code>UIPickerViewDataSource<\/code>\uff0c\u4ee5\u63a7\u5236\u9078\u64c7\u5668\u7684\u884c\u70ba\u548c\u6578\u64da\u3002<\/p>\n<h4>\u81ea\u5b9a\u7fa9 UIPickerViewDelegate<\/h4>\n<pre><code class=\"language-swift line-numbers\">class CustomPickerViewDelegate: NSObject, UIPickerViewDelegate {\n    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {\n        \/\/ \u81ea\u5b9a\u7fa9\u7684\u884c\u70ba\uff0c\u4f8b\u5982\u66f4\u65b0\u754c\u9762\n        print(\"\u9078\u64c7\u4e86\u884c: \\(row) \u5728\u7d44: \\(component)\")\n    }\n}\n<\/code><\/pre>\n<h4>\u81ea\u5b9a\u7fa9 UIPickerViewDataSource<\/h4>\n<pre><code class=\"language-swift line-numbers\">class CustomPickerViewDataSource: NSObject, UIPickerViewDataSource {\n    let items = [\"\u9078\u9805 1\", \"\u9078\u9805 2\", \"\u9078\u9805 3\"] \/\/ \u81ea\u5b9a\u7fa9\u9078\u9805\n\n    func numberOfComponents(in pickerView: UIPickerView) -> Int {\n        return 1 \/\/ \u8fd4\u56de\u7d44\u6578\n    }\n\n    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {\n        return items.count \/\/ \u8fd4\u56de\u9078\u9805\u6578\u91cf\n    }\n\n    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {\n        return items[row] \/\/ \u8fd4\u56de\u9078\u9805\u7684\u986f\u793a\u6587\u5b57\n    }\n}\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 4: \u914d\u7f6e UIPickerView<\/h3>\n<p>\u6700\u5f8c\uff0c\u5c07\u81ea\u5b9a\u7fa9\u7684\u4ee3\u7406\u548c\u6578\u64da\u6e90\u8a2d\u7f6e\u70ba\u4f60\u7684 <code>UIPickerView<\/code>\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">let pickerView = UIPickerView()\npickerView.delegate = CustomPickerViewDelegate()\npickerView.dataSource = CustomPickerViewDataSource()\n<\/code><\/pre>\n<h2>\u5e38\u898b\u932f\u8aa4\u6392\u9664<\/h2>\n<ul>\n<li><strong>\u9078\u9805\u4e0d\u986f\u793a\uff1a<\/strong>\u6aa2\u67e5 <code>numberOfRowsInComponent<\/code> \u8fd4\u56de\u7684\u6578\u91cf\u548c <code>items<\/code> \u9663\u5217\u7684\u9577\u5ea6\u662f\u5426\u5339\u914d\u3002<\/li>\n<li><strong>\u884c\u70ba\u4e0d\u89f8\u767c\uff1a<\/strong>\u78ba\u4fdd <code>delegate<\/code> \u6b63\u78ba\u8a2d\u7f6e\uff0c\u4e14 <code>didSelectRow<\/code> \u65b9\u6cd5\u5167\u6709\u76f8\u61c9\u7684\u908f\u8f2f\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4f60\u53ef\u4ee5\u5c07\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u61c9\u7528\u65bc\u5404\u7a2e\u5834\u666f\uff0c\u4f8b\u5982\u5728\u8a2d\u7f6e\u9801\u9762\u4e2d\u9078\u64c7\u984f\u8272\u3001\u6a23\u5f0f\u6216\u5176\u4ed6\u9078\u9805\uff0c\u8b93\u7528\u6236\u9ad4\u9a57\u66f4\u52a0\u500b\u6027\u5316\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668-\ud83c\udfa8\ud83d\udd25_1675395844.95398.webp\" alt=\"Swift \u81ea\u5b9a\u7fa9\u9078\u64c7\u5668 \ud83c\udfa8\ud83d\udd25\" title=\"Swift \u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\uff0c\u5c55\u793a\u4e86\u7a0b\u5f0f\u8a9e\u6cd5\u8207\u529f\u80fd\u7684\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u7684\u8a2d\u8a08\u3002\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u5728\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u4e2d\u6dfb\u52a0\u5716\u7247\uff1f<\/h3>\n<p>A1: \u4f60\u53ef\u4ee5\u4f7f\u7528 <code>pickerView(_:viewForRow:forComponent:reusing:)<\/code> \u65b9\u6cd5\u4f86\u8fd4\u56de\u4e00\u500b\u5305\u542b\u5716\u7247\u7684\u81ea\u5b9a\u7fa9\u8996\u5716\u3002<\/p>\n<h3>Q2: \u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u7684\u6a23\u5f0f\u5982\u4f55\u4fee\u6539\uff1f<\/h3>\n<p>A2: \u4f60\u53ef\u4ee5\u901a\u904e\u8a2d\u7f6e <code>UIPickerView<\/code> \u7684\u5916\u89c0\u5c6c\u6027\uff0c\u6216\u5728 <code>pickerView(_:titleForRow:forComponent:)<\/code> \u4e2d\u8fd4\u56de\u81ea\u5b9a\u7fa9\u7684\u8996\u5716\u4f86\u4fee\u6539\u6a23\u5f0f\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u8655\u7406\u9078\u64c7\u5668\u7684\u89f8\u63a7\u4e8b\u4ef6\uff1f<\/h3>\n<p>A3: \u4f7f\u7528 <code>didSelectRow<\/code> \u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u7528\u6236\u9078\u64c7\u67d0\u500b\u9078\u9805\u6642\u57f7\u884c\u81ea\u5b9a\u7fa9\u7684\u884c\u70ba\uff0c\u4f8b\u5982\u66f4\u65b0\u754c\u9762\u6216\u89f8\u767c\u5176\u4ed6\u529f\u80fd\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5b78\u7fd2Swift\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\uff0c\u53ef\u4ee5\u8f15\u9b06\u638c\u63e1\u7a0b\u5f0f\u8a9e\u6cd5\u529f\u80fd\uff0c\u8b93\u4f60\u66f4\u5feb\u901f\u5730\u958b\u767c\u51fa\u4f60\u60f3\u8981\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u672c\u6587\u5c07\u4ecb\u7d39Swift\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u958b\u767c\u51fa\u4f60\u60f3\u8981\u7684\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n","protected":false},"author":1,"featured_media":6793,"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-6794","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-\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668-\ud83c\udfa8\ud83d\udd25_1675395844.95398.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1LA","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6794","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=6794"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6794\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6793"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}