我似乎无法设置我的外部pickerview数据源并委托给我的主视图。有没有我可以设定的地方?我想将我的pickerview保留在单独的文件中,因为将来可能还会使用它

这是我的自定义选择器视图

final class LenghtPickerView: UIPickerView, UIPickerViewDataSource, UIPickerViewDelegate {

    let feet = Array(4...7)
    let inches = Array(1...11)

    private var textFieldBeginEdited: UITextField?

    var selectedValue: String {
        get {
            return "\(feet[selectedRow(inComponent: 0)]) ft \(inches[selectedRow(inComponent: 0)]) in"
        }
    }

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return feet.count
        } else {
            return inches.count
        }
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return String(feet[row])
        } else {
            return String(inches[row])
        }
    }
}


在我的主要观点中宣布
private let lenghtPickerView = LenghtPickerView()
也尝试过
private let lenghtPickerView: LenghtPickerView = LenghtPickerView()

无法设置数据源和委托以便呈现数据

lenghtPickerView.delegate = self
lenghtPickerView.dataSource = self

最佳答案

如果您设定
lenghtPickerView.delegate = self
lenghtPickerView.dataSource = self
从您的主视图。
这意味着应在主视图中采用选择器的委托和数据源

关于ios - 我无法设置uipickerview数据源并从外部文件委托(delegate),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58072864/

10-10 23:25