我收到此错误:/Users/natumyers/Desktop/proj/SignUp2ViewController.swift:11:7:类型'SignUp2ViewController'不符合协议'UIPickerViewDataSource'。我正在学习本教程http://codewithchris.com/uipickerview-example/,但我有一个与默认控制器分开的视图控制器。
import UIKit
class SignUp2ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var signuplabel: UITextField!
var labelText = String()
@IBOutlet weak var focusPicker: UIPickerView!
var focusPickerData: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Connect data:
self.focusPicker.delegate = self
self.focusPicker.dataSource = self
focusPickerData = ["type1","type2","type3","type4"]
signuplabel.text = labelText
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
解决方案
这些功能是实现这一目标的关键。我补充说:
// The number of columns of data
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return focusPickerData.count
}
// The data to return for the row and component (column) that's being passed in
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return focusPickerData[row]
}
在didReceiveMemoryWarning函数之后。
答案 0 :(得分:2)
您的视图控制器不符合UIPickerViewDataSource协议。您需要实现这两个必需的方法:
numberOfComponentsInPickerView(_:)
pickerView(_:numberOfRowsInComponent:)
数据源为选择器视图提供组件数量和每个组件中的行数,以显示选取器视图数据。这个协议中的两种方法都是必需的。