{"id":7284,"date":"2025-05-31T12:42:03","date_gmt":"2025-06-05T05:12:02","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7284"},"modified":"2025-06-05T12:42:03","modified_gmt":"2025-06-05T05:12:02","slug":"https-badgameshow-com-steven-314","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-314\/","title":{"rendered":"Swift \u5b8c\u6574\u5be6\u73fe\u8f49\u76e4\u6548\u679c\u6559\u5b78\uff082025 \u5e74\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UIPickerView, \u8f49\u76e4\u6548\u679c, iOS \u958b\u767c, Swift \u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h2>Swift \u5b8c\u6574\u5be6\u73fe\u8f49\u76e4\u6548\u679c\u6559\u5b78\uff082025 \u5e74\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff09<\/h2>\n<p>Swift \u662f\u4e00\u7a2e\u7528\u65bc\u958b\u767c iOS\u3001macOS\u3001watchOS \u548c tvOS \u61c9\u7528\u7a0b\u5f0f\u7684\u958b\u653e\u6e90\u78bc\u7a0b\u5f0f\u8a9e\u8a00\u3002\u5b83\u7531 Apple \u5728 2014 \u5e74 WWDC \u767c\u5e03\uff0c\u65e8\u5728\u53d6\u4ee3 Objective-C\u3002Swift \u5177\u5099\u5b89\u5168\u6027\u3001\u901f\u5ea6\u548c\u6613\u7528\u6027\uff0c\u9069\u5408\u5404\u985e\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u3002\u672c\u7bc7\u6587\u7ae0\u5c07\u5e36\u60a8\u6df1\u5165\u4e86\u89e3\u5982\u4f55\u5728 iOS \u61c9\u7528\u7a0b\u5f0f\u4e2d\u4f7f\u7528 Swift \u5be6\u73fe\u8f49\u76e4\u6548\u679c\u3002<\/p>\n<h3>\u6b65\u9a5f 1\uff1a\u5275\u5efa\u65b0\u7684 Xcode \u5c08\u6848<\/h3>\n<p>\u9996\u5148\uff0c\u6253\u958b Xcode \u4e26\u5275\u5efa\u4e00\u500b\u65b0\u7684\u5c08\u6848\u3002\u9078\u64c7 &#8220;App&#8221; \u6a21\u677f\uff0c\u4e26\u78ba\u4fdd\u9078\u64c7 Swift \u4f5c\u70ba\u8a9e\u8a00\u3002\u5728\u5c08\u6848\u7684\u4e3b\u8996\u5716\u63a7\u5236\u5668\u4e2d\uff0c\u60a8\u9700\u8981\u6dfb\u52a0\u4e00\u500b <strong>UIPickerView<\/strong> \u5143\u4ef6\u3002<\/p>\n<h3>\u6b65\u9a5f 2\uff1a\u6dfb\u52a0 UIPickerView \u5230\u4e3b\u8996\u5716\u63a7\u5236\u5668<\/h3>\n<p>\u5728 Interface Builder \u4e2d\uff0c\u5f9e\u5143\u4ef6\u5eab\u4e2d\u62d6\u66f3 <strong>UIPickerView<\/strong> \u5230\u60a8\u7684\u8996\u5716\u63a7\u5236\u5668\u4e2d\uff0c\u4e26\u8a2d\u7f6e\u9069\u7576\u7684\u7d04\u675f\u689d\u4ef6\u4ee5\u78ba\u4fdd\u5176\u5728\u4e0d\u540c\u8a2d\u5099\u4e0a\u7684\u986f\u793a\u6548\u679c\u3002<\/p>\n<h3>\u6b65\u9a5f 3\uff1a\u8a2d\u7f6e UIPickerView \u7684\u8cc7\u6599\u6e90\u8207\u59d4\u8a17<\/h3>\n<p>\u5275\u5efa\u4e00\u500b\u65b0\u7684\u985e\uff0c\u4e26\u8b93\u5b83\u9075\u5faa <strong>UIPickerViewDataSource<\/strong> \u548c <strong>UIPickerViewDelegate<\/strong> \u5354\u8b70\u3002\u9019\u6a23\u60a8\u5c31\u80fd\u5920\u63a7\u5236\u8f49\u76e4\u7684\u8cc7\u6599\u548c\u884c\u70ba\u3002<\/p>\n<h3>\u6b65\u9a5f 4\uff1a\u5be6\u73fe\u5fc5\u8981\u7684\u5354\u8b70\u65b9\u6cd5<\/h3>\n<p>\u5728\u60a8\u7684\u985e\u4e2d\uff0c\u5be6\u73fe\u4ee5\u4e0b\u5354\u8b70\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong>numberOfComponents(in:)<\/strong>: \u8fd4\u56de\u8f49\u76e4\u4e2d\u7684\u5217\u6578\u3002<\/li>\n<li><strong>numberOfRows(inComponent:)<\/strong>: \u8fd4\u56de\u6bcf\u5217\u4e2d\u7684\u884c\u6578\u3002<\/li>\n<li><strong>titleForRow(_:forComponent:)<\/strong>: \u8fd4\u56de\u6bcf\u884c\u7684\u6a19\u984c\u3002<\/li>\n<li><strong>didSelectRow(_:inComponent:)<\/strong>: \u8655\u7406\u7528\u6236\u9078\u64c7\u7684\u884c\uff0c\u4e26\u57f7\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h3>\u7bc4\u4f8b\u7a0b\u5f0f\u78bc<\/h3>\n<pre><code>import UIKit\n\nclass ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {\n    @IBOutlet weak var pickerView: UIPickerView!\n    \n    let items = [\"\u9078\u9805 1\", \"\u9078\u9805 2\", \"\u9078\u9805 3\", \"\u9078\u9805 4\"]\n    \n    override func viewDidLoad() {\n        super.viewDidLoad()\n        pickerView.dataSource = self\n        pickerView.delegate = self\n    }\n    \n    func numberOfComponents(in pickerView: UIPickerView) -> Int {\n        return 1\n    }\n    \n    func numberOfRows(inComponent component: Int) -> Int {\n        return items.count\n    }\n    \n    func titleForRow(_ row: Int, forComponent component: Int) -> String? {\n        return items[row]\n    }\n    \n    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {\n        print(\"\u9078\u64c7\u4e86\uff1a\\(items[row])\")\n    }\n}<\/code><\/pre>\n<h3>\u6b65\u9a5f 5\uff1a\u6e2c\u8a66\u8f49\u76e4\u6548\u679c<\/h3>\n<p>\u904b\u884c\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u770b\u5230\u8f49\u76e4\u6548\u679c\uff0c\u4e26\u53ef\u4ee5\u9078\u64c7\u4e0d\u540c\u7684\u9078\u9805\u3002\u82e5\u51fa\u73fe\u554f\u984c\uff0c\u8acb\u6aa2\u67e5\u8cc7\u6599\u6e90\u8207\u59d4\u8a17\u662f\u5426\u6b63\u78ba\u9023\u7d50\uff0c\u4ee5\u53ca\u65b9\u6cd5\u7684\u5be6\u73fe\u662f\u5426\u6b63\u78ba\u3002<\/p>\n<h3>\u5e38\u898b\u932f\u8aa4\u6392\u9664<\/h3>\n<ul>\n<li><strong>\u8f49\u76e4\u4e0d\u986f\u793a\u9078\u9805\uff1a<\/strong>\u6aa2\u67e5 <strong>dataSource<\/strong> \u548c <strong>delegate<\/strong> \u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\u3002<\/li>\n<li><strong>\u9078\u9805\u7121\u6cd5\u88ab\u9078\u4e2d\uff1a<\/strong>\u78ba\u4fdd <strong>UIPickerView<\/strong> \u7684\u9ad8\u5ea6\u548c\u5bec\u5ea6\u8a2d\u7f6e\u6b63\u78ba\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5be6\u73fe\u8f49\u76e4\u6548\u679c-\ud83d\udd70\ufe0f_1675410095.388791.webp\" alt=\"Swift \u5be6\u73fe\u8f49\u76e4\u6548\u679c \ud83d\udd70\ufe0f\" title=\"Swift \u8f49\u76e4\u6548\u679c\u3001Swift \u5be6\u73fe\u8f49\u76e4\u3001Swift \u8f49\u76e4\u7a0b\u5f0f\u78bc\u3001Swift \u8f49\u76e4\u61c9\u7528\u3001Swift \u8f49\u76e4\u793a\u4f8b\" width=\"1200\" height=\"628\"\/><\/p>\n<h3>\u5ef6\u4f38\u61c9\u7528<\/h3>\n<p>\u8f49\u76e4\u6548\u679c\u53ef\u4ee5\u61c9\u7528\u5728\u8a31\u591a\u4e0d\u540c\u7684\u5834\u666f\u4e2d\uff0c\u4f8b\u5982\u9078\u64c7\u984f\u8272\u3001\u65e5\u671f\u6216\u6642\u9593\u3002\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u8981\u64f4\u5c55 <strong>UIPickerView<\/strong> \u7684\u8cc7\u6599\u6e90\uff0c\u4ee5\u6eff\u8db3\u60a8\u7684\u61c9\u7528\u9700\u6c42\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u81ea\u5b9a\u7fa9 UIPickerView \u7684\u5916\u89c0\uff1f<\/h3>\n<p>A1: \u60a8\u53ef\u4ee5\u4f7f\u7528 <strong>pickerView(_:viewForRow:forComponent:reusing:)<\/strong> \u65b9\u6cd5\u4f86\u81ea\u5b9a\u7fa9\u6bcf\u4e00\u884c\u7684\u8996\u5716\uff0c\u9019\u6a23\u53ef\u4ee5\u66f4\u6539\u5b57\u578b\u3001\u984f\u8272\u6216\u5176\u4ed6\u5c6c\u6027\u3002<\/p>\n<h3>Q2: UIPickerView \u652f\u63f4\u591a\u5217\u55ce\uff1f<\/h3>\n<p>A2: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728 <strong>numberOfComponents(in:)<\/strong> \u65b9\u6cd5\u4e2d\u8fd4\u56de\u591a\u65bc\u4e00\u5217\uff0c\u4e26\u5728 <strong>numberOfRows(inComponent:)<\/strong> \u65b9\u6cd5\u4e2d\u70ba\u6bcf\u5217\u63d0\u4f9b\u6578\u64da\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u5c07\u9078\u64c7\u7684\u503c\u50b3\u905e\u5230\u5176\u4ed6\u8996\u5716\uff1f<\/h3>\n<p>A3: \u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7406\u6a21\u5f0f\u6216\u56de\u8abf\u51fd\u6578\u5c07\u9078\u64c7\u7684\u503c\u50b3\u905e\u5230\u5176\u4ed6\u8996\u5716\u63a7\u5236\u5668\uff0c\u9019\u6a23\u53ef\u4ee5\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u61c9\u7528\u908f\u8f2f\u3002<\/p>\n<p>\u900f\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u5df2\u7d93\u5b78\u6703\u5982\u4f55\u4f7f\u7528 Swift \u5be6\u73fe\u8f49\u76e4\u6548\u679c\uff0c\u4e26\u638c\u63e1\u4e86\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u5c0d\u60a8\u6709\u6240\u5e6b\u52a9\uff01<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u5be6\u73fe\u8f49\u76e4\u6548\u679c\uff0c\u4e26\u63d0\u4f9b\u7c21\u55ae\u6613\u61c2\u7684\u7a0b\u5f0f\u78bc\u793a\u4f8b\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n","protected":false},"author":1,"featured_media":7283,"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-7284","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-\u5be6\u73fe\u8f49\u76e4\u6548\u679c-\ud83d\udd70\ufe0f_1675410095.388791.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Tu","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7284","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=7284"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7284\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7283"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}