Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple UIPickerView subclass
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 numberOfComponentsInPickerView(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: pickerView, row: row, title: titles[row])
}
}
let model: SimplePickerViewModel
init(titles: [String], selectionHandler: ((pickerView: UIPickerView, row: Int, title: String) -> ())? = nil) {
self.model = SimplePickerViewModel(titles: titles, selectionHandler: selectionHandler)
println("titles: \(titles)")
super.init(frame: CGRectZero)
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