swift-UIPickerView(选择控件)

swift-UIPickerView(选择控件),第1张

概述  import UIKit//UIPickerView 的委托协议是 UIPickerViewDelegate,数据源是 UIPickerViewDataSource。我们需要在视图控制器中声明实现 UIPiekerViewDelegate 和 UIPickerViewDataSource 协议。class RootViewController: UIViewController,

 

import UIKit//UIPickerVIEw 的委托协议是 UIPickerVIEwDelegate,数据源是 UIPickerVIEwDataSource。我们需要在视图控制器中声明实现 UIPIEkerVIEwDelegate 和 UIPickerVIEwDataSource 协议。class RootVIEwController: UIVIEwController,UIPickerVIEwDelegate,UIPickerVIEwDataSource {    var label: UILabel!    var pickerVIEw: UIPickerVIEw!        var pickerData:[String: [String]] = ["放假":["写代码","玩游戏","泡妹子"],"旅游":["马尔代夫","火星","迪拜","月球"],"上班":["加班","不加班"]] //保存全部数据    var pickerProvincesData: [String] = ["放假","旅游","上班"] //第一级数据    var pickerCitIEsData: [String] = ["写代码","泡妹子"]//第二级数据            overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        // 创建视图        let screen = UIScreen.main.bounds        // 设置 根视图背景色        self.vIEw.backgroundcolor = UIcolor.white         // 选择器        let pickerVIEwWIDth:CGfloat = 320        let pickerVIEwHeight:CGfloat = 162        self.pickerVIEw = UIPickerVIEw(frame: CGRect(x:0,y: 0,wIDth: pickerVIEwWIDth,height: pickerVIEwHeight))        //因为该Controller中实现了UIPickerVIEwDataSource接口所以将dataSource设置成自己        self.pickerVIEw.dataSource = self        //将delegate设置成自己        self.pickerVIEw.delegate = self        self.vIEw.addSubvIEw(self.pickerVIEw)                        // 添加标签        let labelwIDth:CGfloat = 200        let labelheight:CGfloat = 21        let labeltopVIEw:CGfloat = 281        self.label = UILabel(frame: CGRect(x:(screen.size.wIDth - labelwIDth)/2,y: labeltopVIEw,wIDth: labelwIDth,height: labelheight))        self.label.text = "Label"        // 字体左右居中        self.label.textAlignment = .center        self.vIEw.addSubvIEw(self.label)                        // button 按钮        let button = UIbutton(type: .system)        button.setTitle("button",for: UIControlState.normal)        let buttonwIDth:CGfloat = 46        let buttonheight:CGfloat = 30        let buttontopVIEw:CGfloat = 379        button.frame = CGRect(x: (screen.size.wIDth - buttonwIDth)/2,y: buttontopVIEw,wIDth: buttonwIDth,height: buttonheight)        //事件        button.addTarget(self,action: #selector(onclick(_:)),for: .touchUpInsIDe)        self.vIEw.addSubvIEw(button)            }    //设置选择框的总列数,继承于UIPickVIEwDataSource协议    func numberOfComponents(in pickerVIEw: UIPickerVIEw) -> Int {        return 2    }    //设置选择框的总行数,继承于UIPickVIEwDataSource协议    func pickerVIEw(_ pickerVIEw: UIPickerVIEw,numberOfRowsInComponent component: Int) -> Int {        //总行数设置为数据源的总长度。component :为0 表示第一列,1 表示第二列        //根据不同的数据源设置不同的个数        if(component == 0){            return self.pickerProvincesData.count        }else{            return self.pickerCitIEsData.count        }    }    //设置选项框各选项的内容,继承于UIPickVIEwDelegate协议    func pickerVIEw(_ pickerVIEw: UIPickerVIEw,TitleForRow row: Int,forComponent component: Int) -> String?{        if(component == 0){ //选择第一级数据            return self.pickerProvincesData[row]        }        else{//选择第二级数据            return self.pickerCitIEsData[row]        }    }    //选择控件的事件选择    func pickerVIEw(_ pickerVIEw: UIPickerVIEw,dIDSelectRow row: Int,inComponent component: Int) {        if(component == 0){            //记录用户选择的值            let selectedProvince = self.pickerProvincesData[row] as String            // 根据第一列选择的值,获取第二列数据            self.pickerCitIEsData = self.pickerData[selectedProvince]!            //刷新第二列的数据源            self.pickerVIEw.reloadComponent(1)            //刷新数据源后将第二组数据转到下标为0,并且开启动画效果            self.pickerVIEw.selectRow(0,inComponent: 1,animated: true)        }    }    //设置每行选项的高度    func pickerVIEw(_ pickerVIEw: UIPickerVIEw,rowHeightForComponent component: Int) -> CGfloat {        return 45.0    }    // 获取值    @objc func onclick(_ sender: AnyObject) {        //获得2列选取值的下标        let row1 = self.pickerVIEw.selectedRow(inComponent: 0)        let row2 = self.pickerVIEw.selectedRow(inComponent: 1)        // 根据下标获取值        let selected1 = self.pickerProvincesData[row1] as String        let selected2 = self.pickerCitIEsData[row2] as String        //拼接值        let Title = String(format: "%@,%@",selected1,selected2)        self.label.text = Title    }}

 

总结

以上是内存溢出为你收集整理的swift-UIPickerView(选择控件)全部内容,希望文章能够帮你解决swift-UIPickerView(选择控件)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1018761.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存