微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift - UIPickerView

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐