选择器视图未显示数组数据

时间:2019-04-29 08:30:04

标签: swift

var Months = ["A","B","C","D"]
var pickerview = UIPickerView()
@IBOutlet var txt_Month: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    pickerview.isHidden = true
    pickerview.dataSource = self
    pickerview.delegate =  self

    txt_Month.inputView = pickerview
} 

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return Months.count
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
   txt_Month.text = Months[row]
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
    return Months[row]
}

2 个答案:

答案 0 :(得分:1)

也许是因为pickerview被隐藏了吗?

答案 1 :(得分:0)

设置pickerview为未隐藏。

pickerview.isHidden = false