• Swift


    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,  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)
        }
    }
    
  • 相关阅读:
    mysql 按天创建分区存储过程
    Logstash下载安装使用并日志写入Mysql数据库
    开源BI分析工具Metabase配置与完全使用手册
    MySQL安装之yum安装
    IDEA创建SpringBoot
    JDK环境变量配置
    MySQL存储过程
    定时执行任务
    fastjson的使用——JSON字符串、JSON对象、Java对象的互转
    SQL反模式读书笔记思维导图
  • 原文地址:https://www.cnblogs.com/baidaye/p/9183328.html
Copyright © 2020-2023  润新知