透過滾動提供使用者選擇的一種View,必須實作UIPickerDataSource與UIPickerViewDelegate兩個Protocol才可使用,
extension ViewController: UIPickerViewDataSource {
///總共有幾個Component
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
///每個Component有幾個Row
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 3
}
}
extension ViewController: UIPickerViewDelegate {
///所顯示的Title
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "\(component), \(row)"
}
}
最後別忘了指定成self
pickerView.dataSource = self
pickerView.delegate = self
你可以透過此函式來得知使用者是選擇哪一個Row
pickerView.selectedRow(inComponent: 0) //第0個Component選擇了第幾個Row
沒有留言:
張貼留言