
import UIKitclass VIEwController: UIVIEwController,UIPickerVIEwDataSource,UIPickerVIEwDelegate { var pickerVIEw:UIPickerVIEw! overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() pickerVIEw = UIPickerVIEw(frame: CGRect(x: 0,y: 250,wIDth: 350,height:100)) //delegate设为自己 pickerVIEw.delegate = self //DataSource设为自己 pickerVIEw.dataSource = self //设置PickerVIEw默认值 pickerVIEw.selectRow(1,inComponent: 0,animated: true) self.vIEw.addSubvIEw(pickerVIEw) let button = UIbutton(frame:CGRect(x: 0,y: 0,wIDth: 100,height: 30)) button.frame = CGRect(x: 150,y: 400,wIDth: 50,height: 50) button.backgroundcolor = UIcolor.red button.setTitle("确定",for: .normal) button.addTarget(self,action: #selector(VIEwController.getPickerVIEwValue),for: .touchUpInsIDe) self.vIEw.addSubvIEw(button) } @objc func getPickerVIEwValue(){ let message = String(pickerVIEw.selectedRow(inComponent: 0)) let alret = UIAlertController(Title: "选择",message: message,preferredStyle: .alert) self.present(alret,animated: true,completion: nil) dispatchQueue.main.asyncAfter(deadline: dispatchTime.Now() + 1) { self.presentedVIEwController?.dismiss(animated: true,completion: nil) } } //设置PickerVIEw列数(dataSourse协议) func numberOfComponents(in pickerVIEw: UIPickerVIEw) -> Int { return 3 } //设置PickerVIEw行数(dataSourse协议) func pickerVIEw(_ pickerVIEw: UIPickerVIEw,numberOfRowsInComponent component: Int) -> Int { return 9 } //设置PickerVIEw选项内容(delegate协议) func pickerVIEw(_ pickerVIEw: UIPickerVIEw,TitleForRow row: Int,forComponent component: Int) -> String? { return String(row)+"-"+String(component) } //设置列宽 func pickerVIEw(_ pickerVIEw: UIPickerVIEw,wIDthForComponent component: Int) -> CGfloat { if component == 0{ return 100 } else{ return 200 } } //设置行高 func pickerVIEw(_ pickerVIEw: UIPickerVIEw,rowHeightForComponent component: Int) -> CGfloat { return 50 } //血钙PickerVIEw选项 func pickerVIEw(_ pickerVIEw: UIPickerVIEw,vIEwForRow row: Int,forComponent component: Int,reusing vIEw: UIVIEw?) -> UIVIEw { //将图片设为PickerVIEw选型 let image = UIImage(named: "icon"+String(row)) let imageVIEw = UIImageVIEw(image:image) //修改字体,大小,颜色 var pickerLabel = vIEw as? UILabel if pickerLabel == nil{ pickerLabel = UILabel() pickerLabel?.Font = UIFont.systemFont(ofSize: 16) pickerLabel?.textAlignment = .center } pickerLabel?.text = String(row)+"-"+String(component) pickerLabel?.textcolor = UIcolor.blue return imageVIEw//pickerLabel!(选择其一) } //检测响应选项的选择状态 func pickerVIEw(_ pickerVIEw: UIPickerVIEw,dIDSelectRow row: Int,inComponent component: Int) { print(String(row)+"-"+String(component)) }} 总结 以上是内存溢出为你收集整理的Swift - UIPickerView全部内容,希望文章能够帮你解决Swift - UIPickerView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)