1

我创建了一个可重用的控件,用于我正在处理的项目中。它只是 aUITextField将 a 显示UIPickerView为它的inputView

在此处输入图像描述

class InputPickerView: UIView {
    @IBOutlet private var view: UIView!
    @IBOutlet weak private var titleLabel: UILabel!
    @IBOutlet weak private var textField: UITextField!

    private(set) var pickerView = UIPickerView()

    var options: [String] = []

    var option: String {
        get {
            return textField.text ?? ""
        }
        set {
            textField.text = newValue
        }
    }

    var title: String = "" {
        didSet {
            titleLabel.text = title
        }
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    private func commonInit() {
        Bundle.main.loadNibNamed("InputPickerView", owner: self, options: nil)
        addSubview(view)
        view.frame = bounds
        view.autoresizingMask = [.flexibleHeight, .flexibleWidth]

        pickerView.dataSource = self
        pickerView.delegate = self
        textField.inputView = pickerView
    }
}

extension InputPickerView: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return false
    }
}

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

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

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

extension InputPickerView: UIPickerViewDelegate {
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        textField.text = options[row]
    }
}

目前它只接受一个字符串数组并返回一个字符串。我试图通过在泛型的帮助下使其接受/返回任何类型(例如结构和枚举)来使其更具可重用性。我希望使结构/枚举符合CustomStringConvertible并使用描述属性值作为选择器视图选项的显示值。

但我很难弄清楚如何做到这一点。我遇到的所有文章、问题、教程都涉及协议。所以我有点困惑。

如何使optionsandoption变量接受/返回具有泛型的任何类型?

我的意思是,假设我创建了一个名为State.

struct State {
    let id: Int
    let title: String
}

extension State: CustomStringConvertible {
    var description: String {
        return title
    }
}

我没有将字符串传递给视图,而是试图让它接受属性中的State对象实例,options并让视图使用该description值作为显示值。当用户选择一个时,它会通过属性返回选择的State对象。option

演示项目

4

1 回答 1

1

首先,您需要一个协议,从您的符合类型中提取字符串以显示在选择器中:

protocol Presentable {
    var title: String { get }
}

使您的State结构符合Presentable

struct State: Presentable {
    let id: Int
    let title: String
}

为您添加一些通用约束InputPickerView,当您需要模型中的文本时,只需引用该title属性即可。请注意,如果您使用泛型,则不能再为您的UIPickerViewDataSourceUIPickerViewDelegate方法创建扩展。

class InputPickerView<OptionType: Presentable>: UIView, UIPickerViewDataSource, UIPickerViewDelegate {

    private var titleLabel: UILabel!
    private var textField: UITextField!

    var options: [OptionType] = []

    var selectedOption: OptionType?

    var title: String = "" {
        didSet {
            titleLabel.text = title
        }
    }

    // ... Other stuff you need to add ...

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

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

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

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        textField.text = options[row].title
        selectedOption = options[row]
    }
}

您可以InputPickerView这样创建:

let pickerView = InputPickerView<State>()
pickerView.options = [
    State(id: 1, title: "First"),
    State(id: 2, title: "Second"),
    State(id: 3, title: "Third"),
]
于 2020-04-30T05:11:42.983 回答