Swift - UIPickerView

Posted ios-development

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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))
    }
}

以上是关于Swift - UIPickerView的主要内容,如果未能解决你的问题,请参考以下文章

将 UIPickerView 选择保存到 NSUserDefaults swift

swift3.0 三级联动UIPickerView城市选择器

使用 Swift 使用 Core Data 对象填充 UIPickerView

Swift - UIPickerView 子视图中没有出现工具栏完成按钮

想要在 UIAlertController 或 UIPopoverController Swift 中显示 UIPickerView?

Swift - UIPickerView 的带有完成按钮的 UIToolbar 在横向上消失