Swift - UIPickerView

import UIKit
class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
    
    var pickerView:UIPickerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        pickerView = UIPickerView()
        pickerView.dataSource = self
        pickerView.delegate = self
        pickerView.selectRow(1, inComponent: 0, animated: true)
        pickerView.selectRow(2, inComponent: 1, animated: true)
        pickerView.selectRow(3, inComponent: 2, animated: true)
        self.view.addSubview(pickerView)
        
        
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
        button.center = self.view.center
        button.backgroundColor = UIColor.blue
        button.setTitle("获取信息", for:.normal)
        button.addTarget(self, action: #selector(ViewController.getPickerValue), for: .touchUpInside)
        self.view.addSubview(button)
        
        
    }
    //设置选择框的列数为3列,继承于UIPickerViewDataSource协议
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    //设置选择框的行数为9行,继承于UIPickerViewDataSource协议
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 9
    }
   //设置选择框各选项的内容,继承于UIPickerViewDelegate协议
    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 80
        }else{//其他变窄
            return 50
        }
    }
    //设置行高
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }
    //任意UIView类型的元素
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
       var pickerLabel = view as? UILabel
        if pickerLabel == nil{
           pickerLabel = UILabel()
            pickerLabel?.font = UIFont.systemFont(ofSize: 13)
            pickerLabel?.textAlignment = .center
        }
        pickerLabel?.text = String(row) + "-" + String(component)
        pickerLabel?.textColor = UIColor.blue
        return pickerLabel!
        
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        print(component,row)
    }
    
    @objc func getPickerValue(){
     let message = String(pickerView.selectedRow(inComponent: 0))+"-"
        + String(pickerView.selectedRow(inComponent: 1))+"-"
        + String(pickerView.selectedRow(inComponent: 2))
      let alertController = UIAlertController(title: "被选中的索引为", message: message, preferredStyle: .alert)
      let okAction = UIAlertAction(title: "ok", style: .default, handler: nil)
      alertController .addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
}

 

posted @ 2018-06-14 15:40  小白姐  阅读(378)  评论(0编辑  收藏  举报