加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift - UIPickerView

发布时间:2020-12-14 05:07:00 所属栏目:百科 来源:网络整理
导读:import UIKitclass ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate { var pickerView:UIPickerView! override func viewDidLoad() { super.viewDidLoad() pickerView = UIPickerView(frame: CGRect(x: 0,y: 250,width: 350,
import UIKit

class 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))
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读