Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Harpreet Singh
359 PointsUsing protocol for passing data
I am trying to make a custom class of a UIView and UIPickerViewDelegates/DataSource, i want to bring some data from UIViewController threw the protocols for example a function which take an array to give data for UIPickerView
so code may be look this:
import UIKit
protocol PickerViewData: NSObjectProtocol {
func getData() -> [String]
}
class CustomPicker: UIView {
let pickerView = UIPickerView()
var delegate:PickerViewData?
var getData:[String] = []
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func setDelegate(tdelegate:PickerViewData){
self.delegate = tdelegate
}
private func setupView(){
pickerView.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: 200)
pickerView.delegate = self
pickerView.dataSource = self
addSubview(pickerView)
}
}
extension CustomPicker: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return getData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return getData[row]
}
}
so when UIViewController adopted PickerViewData protocol and return and array with strings threw the function how can i use that function to make my getData array = new array