Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple UIPickerView subclass upgrade for swift 4
import UIKit
class SimplePickerView : UIPickerView {
class SimplePickerViewModel : NSObject, UIPickerViewDelegate, UIPickerViewDataSource {
var titles: [String]
var selectionHandler: ((_ pickerView: UIPickerView, _ row: Int, _ title: String) -> ())?
init(titles: [String], selectionHandler: ((_ pickerView: UIPickerView, _ row: Int, _ title: String) -> ())? = nil) {
self.titles = titles
self.selectionHandler = selectionHandler
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return titles.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return titles[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectionHandler?(pickerView, row, titles[row])
}
}
let model: SimplePickerViewModel
init(titles: [String], selectionHandler: ((_ pickerView: UIPickerView, _ row: Int, _ title: String) -> ())? = nil) {
self.model = SimplePickerViewModel(titles: titles, selectionHandler: selectionHandler)
print("titles: \(titles)")
super.init(frame: CGRect.zero)
self.delegate = model
self.dataSource = model
}
required init(coder aDecoder: NSCoder) {
self.model = SimplePickerViewModel(titles: [], selectionHandler: nil)
super.init(coder: aDecoder)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment