{"id":6059,"date":"2022-06-08T16:48:48","date_gmt":"2022-06-08T08:48:48","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6059"},"modified":"2022-06-09T09:49:45","modified_gmt":"2022-06-09T01:49:45","slug":"swift-%e9%80%89%e6%8b%a9%e6%a1%86%ef%bc%88uipickerview%ef%bc%89%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6059","title":{"rendered":"Swift &#8211; \u9009\u62e9\u6846\uff08UIPickerView\uff09\u7684\u7528\u6cd5"},"content":{"rendered":"\n<p><strong>1\uff0c\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n\n\n\n<p>\u9009\u62e9\u6846\u53ef\u4ee5\u8ba9\u7528\u6237\u4ee5\u6ed1\u52a8\u7684\u65b9\u5f0f\u9009\u62e9\u503c\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<br><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-12.png\" alt=\"\" class=\"wp-image-6060\" width=\"282\" height=\"234\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-12.png 518w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-12-300x249.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-12-230x191.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-12-350x291.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-12-480x398.png 480w\" sizes=\"(max-width: 282px) 100vw, 282px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\n \nclass ViewController:UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{\n     \n    var pickerView:UIPickerView!\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n        pickerView = UIPickerView()\n        \/\/\u5c06dataSource\u8bbe\u7f6e\u6210\u81ea\u5df1\n        pickerView.dataSource = self\n        \/\/\u5c06delegate\u8bbe\u7f6e\u6210\u81ea\u5df1\n        pickerView.delegate = self\n        \/\/\u8bbe\u7f6e\u9009\u62e9\u6846\u7684\u9ed8\u8ba4\u503c\n        pickerView.selectRow(1,inComponent:0,animated:true)\n        pickerView.selectRow(2,inComponent:1,animated:true)\n        pickerView.selectRow(3,inComponent:2,animated:true)\n        self.view.addSubview(pickerView)\n         \n        \/\/\u5efa\u7acb\u4e00\u4e2a\u6309\u94ae\uff0c\u89e6\u6478\u6309\u94ae\u65f6\u83b7\u5f97\u9009\u62e9\u6846\u88ab\u9009\u62e9\u7684\u7d22\u5f15\n        let button = UIButton(frame:CGRect(x:0, y:0, width:100, height:30))\n        button.center = self.view.center\n        button.backgroundColor = UIColor.blue\n        button.setTitle(\"\u83b7\u53d6\u4fe1\u606f\",for:.normal)\n        button.addTarget(self, action:#selector(ViewController.getPickerViewValue),\n                         for: .touchUpInside)\n        self.view.addSubview(button)\n    }\n    \/\/\u8bbe\u7f6e\u9009\u62e9\u6846\u7684\u5217\u6570\u4e3a3\u5217,\u7ee7\u627f\u4e8eUIPickerViewDataSource\u534f\u8bae\n    func numberOfComponents(in pickerView: UIPickerView) -> Int {\n        return 3\n    }\n     \n    \/\/\u8bbe\u7f6e\u9009\u62e9\u6846\u7684\u884c\u6570\u4e3a9\u884c\uff0c\u7ee7\u627f\u4e8eUIPickerViewDataSource\u534f\u8bae\n    func pickerView(_ pickerView: UIPickerView,\n                    numberOfRowsInComponent component: Int) -> Int {\n        return 9\n    }\n     \n    \/\/\u8bbe\u7f6e\u9009\u62e9\u6846\u5404\u9009\u9879\u7684\u5185\u5bb9\uff0c\u7ee7\u627f\u4e8eUIPickerViewDelegate\u534f\u8bae\n    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int,\n                    forComponent component: Int) -> String? {\n        return String(row)+\"-\"+String(component)\n    }\n     \n    \/\/\u89e6\u6478\u6309\u94ae\u65f6\uff0c\u83b7\u5f97\u88ab\u9009\u4e2d\u7684\u7d22\u5f15\n    @objc func getPickerViewValue(){\n        let message = String(pickerView.selectedRow(inComponent: 0)) + \"-\"\n            + String(pickerView!.selectedRow(inComponent: 1)) + \"-\"\n            + String(pickerView.selectedRow(inComponent: 2))\n        let alertController = UIAlertController(title: \"\u88ab\u9009\u4e2d\u7684\u7d22\u5f15\u4e3a\",\n                                                message: message, preferredStyle: .alert)\n        let okAction = UIAlertAction(title: \"OK\", style: .cancel, handler: nil)\n        alertController.addAction(okAction)\n        self.present(alertController, animated: true, completion: nil)\n    }\n}<\/pre>\n\n\n\n<p><strong>2\uff0c\u8c03\u6574\u9009\u62e9\u6846\u7684\u5c3a\u5bf8<\/strong><\/p>\n\n\n\n<p><strong>UIPickerView <\/strong>\u7528 <strong>frame <\/strong>\u548c <strong>center <\/strong>\u4e24\u4e2a\u5c5e\u6027\u8bbe\u7f6e\u6574\u4e2a\u9009\u62e9\u6846\u7684\u5927\u5c0f\u548c\u4f4d\u7f6e\u3002<br>\u5982\u679c\u8981\u8c03\u6574\u5185\u90e8\u5217\u7684\u5bbd\u5ea6\uff0c\u9700\u8981\u5b9e\u73b0 <strong>UIPickerViewDelegate <\/strong>\u534f\u8bae\u7c7b\u4e2d <strong>pickerView:widthForComponent<\/strong> \u65b9\u6cd5\u8bbe\u7f6e<br>\u5982\u679c\u8981\u8c03\u6574\u5185\u90e8\u884c\u9ad8\uff0c\u5219\u9700\u8981\u5b9e\u4e60\u4e0a\u8ff0\u534f\u8bae\u7c7b\u4e2d <strong>pickerView:rowHeightForComponent<\/strong> \u65b9\u6cd5\u8bbe\u7f6e<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\u8bbe\u7f6e\u5217\u5bbd\nfunc pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {\n    if(0 == component){\n        \/\/\u7b2c\u4e00\u5217\u53d8\u5bbd\n        return 100\n    }else{\n        \/\/\u7b2c\u4e8c\u3001\u4e09\u5217\u53d8\u7a84\n        return 30\n    }\n}\n \n\/\/\u8bbe\u7f6e\u884c\u9ad8\nfunc pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int)\n    -> CGFloat {\n    return 50\n}<\/pre>\n\n\n\n<p><strong>3\uff0c\u5c06\u56fe\u7247\u4f5c\u4e3a\u9009\u62e9\u6846\u9009\u9879<\/strong><\/p>\n\n\n\n<p>\u9009\u62e9\u6846\u9009\u9879\u7684\u5185\u5bb9\uff0c\u9664\u4e86\u53ef\u4ee5\u4f7f\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\uff0c\u8fd8\u53ef\u4ee5\u662f\u4efb\u610fUIView\u7c7b\u578b\u7684\u5143\u7d20\u3002\u6bd4\u5982\u6211\u4eec\u5c06\u9009\u9879\u5185\u5bb9\u8bbe\u7f6e\u4e3a\u56fe\u7247\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int,\n                reusing view: UIView?) -> UIView {\n    let image = UIImage(named:\"icon_\"+String(row))\n    let imageView = UIImageView()\n    imageView.image = image\n    return imageView\n}<\/pre>\n\n\n\n<p><strong>4\uff0c\u68c0\u6d4b\u54cd\u5e94\u9009\u9879\u7684\u9009\u62e9\u72b6\u6001<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int,\n                inComponent component: Int) {\n    \/\/\u5c06\u5728\u6ed1\u52a8\u505c\u6b62\u540e\u89e6\u53d1\uff0c\u5e76\u6253\u5370\u51fa\u9009\u4e2d\u5217\u548c\u884c\u7d22\u5f15\n    print(component)\n    print(row)\n}<\/pre>\n\n\n\n<p><strong>5\uff0c\u4fee\u6539\u9009\u9879\u7684\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-13.png\" alt=\"\" class=\"wp-image-6061\" width=\"426\" height=\"298\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-13.png 634w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-13-300x210.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-13-230x161.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-13-350x245.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-13-480x336.png 480w\" sizes=\"(max-width: 426px) 100vw, 426px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func pickerView(_ pickerView: UIPickerView, viewForRow row: Int,\n                forComponent component: Int, reusing view: UIView?) -> UIView {\n    var pickerLabel = view as? UILabel\n    if pickerLabel == nil {\n        pickerLabel = UILabel()\n        pickerLabel?.font = UIFont.systemFont(ofSize: 13)\n        pickerLabel?.textAlignment = .center\n    }\n    pickerLabel?.text = String(row)+\"-\"+String(component)\n    pickerLabel?.textColor = UIColor.blue\n    return pickerLabel!\n}<\/pre>\n\n\n\n<p><strong>6\uff0cSwiftUI\u4e2d\u4f7f\u7528UIPickerView<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-14.png\" alt=\"\" class=\"wp-image-6063\" width=\"362\" height=\"241\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-14.png 656w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-14-300x200.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-14-230x154.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-14-350x234.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-14-480x320.png 480w\" sizes=\"(max-width: 362px) 100vw, 362px\" \/><\/figure><\/div>\n\n\n\n<p>\u5728SwiftUI\u4e2d\u5b9a\u4e49\u4e00\u4e2a UIPickerView <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct PickerView: UIViewRepresentable {\n    \n    @Binding var selectionText1: String\n    @Binding var selectionText2: String\n    @Binding var selectionText3: String\n    @Binding var selectionText4: String\n    \n    func makeUIView(context: Context) -> UIPickerView {\n        \n        let view = UIPickerView()\n        \n        view.delegate = context.coordinator\n        view.dataSource = context.coordinator\n        \n        view.selectRow(Int(selectionText1)!, inComponent: 0, animated: true)\n        view.selectRow(Int(selectionText2)!, inComponent: 1, animated: true)\n        view.selectRow(Int(selectionText3)!, inComponent: 2, animated: true)\n        view.selectRow(Int(selectionText4)! \/ 100, inComponent: 3, animated: true)\n        return view\n    }\n    \n    func updateUIView(_ uiView: UIPickerView, context: Context) {}\n    \n    func makeCoordinator() -> PickerView.Coordinator {\n        Coordinator(self)\n    }\n    \n    class Coordinator: NSObject, UIPickerViewDelegate, UIPickerViewDataSource {\n        var control: PickerView\n        \n        init(_ control: PickerView) {\n            self.control = control\n        }\n        \n        func numberOfComponents(in pickerView: UIPickerView) -> Int {\n            return 4\n        }\n        \n        func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {\n            if component == 3 {\n                return 80\n            } else {\n                return 50\n            }\n        }\n\n        func pickerView(_ pickerView: UIPickerView,\n                        numberOfRowsInComponent component: Int) -> Int\n        {\n            return 10\n        }\n        \n        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int,\n                        inComponent component: Int) {\n            if component == 0 {\n                self.control.selectionText1 = String(row)\n            } else if component == 1 {\n                self.control.selectionText2 = String(row)\n            } else if component == 2 {\n                self.control.selectionText3 = String(row)\n            } else {\n                self.control.selectionText4 = String(row * 100)\n            }\n        }\n        \n        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int,\n                        forComponent component: Int) -> String?\n        {\n            if component != 3 {\n                return String(row)\n            } else {\n                return String(row * 100)\n            }\n        }\n    }\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct KMPickerView: View {\n\n    @Binding var selectionText1: String\n    @Binding var selectionText2: String\n    @Binding var selectionText3: String\n    @Binding var selectionText4: String\n    @Binding var selectedKilo: Bool\n    \n    var body: some View {\n        ZStack(alignment: .trailing) {\n            HStack {\n                Button(action: {\n                    withAnimation {\n                        self.selectedKilo = false\n                    }\n                    \n                }) {\n                    VStack {\n                        Text(\"\u78ba\u5b9a\")\n                    }.frame(width: 50, height: 30).cornerRadius(5)\n                        \n                }.cornerRadius(5)\n            }.zIndex(40).offset(x: -10, y: -80)\n            \n            Text(\"k\").zIndex(40).offset(x: -115)\n            PickerView(selectionText1: self.$selectionText1, selectionText2: self.$selectionText2, selectionText3: self.$selectionText3, selectionText4: self.$selectionText4).zIndex(30)\n            Text(\"m\").zIndex(40).offset(x: -25)\n        }.frame(alignment: .trailing)\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\uff0c\u57fa\u672c\u7528\u6cd5 \u9009\u62e9\u6846\u53ef\u4ee5\u8ba9\u7528\u6237\u4ee5\u6ed1\u52a8\u7684\u65b9\u5f0f\u9009\u62e9\u503c\u3002\u793a\u4f8b\u5982\u4e0b\uff1a 2\uff0c\u8c03\u6574\u9009\u62e9\u6846\u7684\u5c3a\u5bf8 UIPickerView  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6059"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6059"}],"version-history":[{"count":6,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6059\/revisions"}],"predecessor-version":[{"id":6068,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6059\/revisions\/6068"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}