UIPickerView 和 UIDatePicker

UIPickerView 和 UIDatePicker_第1张图片
图.png

UIDatePicker

        //UIDatePicker属于UIContrl 类似于UIButton 可以运用于12306时间限制
        let date = UIDatePicker(frame:CGRect(x: 50, y: 400, width: 400, height: 200))
        date.datePickerMode = .Time
        self.view.addSubview(date)
        date.addTarget(self, action: #selector(change(_:)), forControlEvents: .ValueChanged)
    func change(da:UIDatePicker){
        
        print(da.date)
    }

UIPickerView

思路 几列 每列里面包含了几行 每行里面的数据

 //UIPickerView属于UIView 类似UITableView 用协议UIPickerViewDataSource,UIPickerViewDelegate实现数据和触发事件
        let picker = UIPickerView(frame: CGRect(x: 50, y: 100, width: 300, height: 200))
        picker.dataSource = self
        picker.delegate = self
        self.view.addSubview(picker)

实现协议

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 3
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 8
    }
 func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
        if component == 0{
            let brownView = UIImageView()
            brownView.image = UIImage(named: "图")
            return brownView
        }
        else if component == 1{
            let redView = UIView()
            redView.backgroundColor = UIColor.redColor()
            return redView
            
        }
        else{
            let yellowiew = UIView()
            yellowiew.backgroundColor = UIColor.yellowColor()
            return yellowiew
        }
        
    }
    
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        print("\(component):","\(row)")
    }

你可能感兴趣的:(UIPickerView 和 UIDatePicker)