I'm using swift 2.0 (I'm also new to Swift, and iOS development) ...
I'd like to have a view controller that has two UIPickers. Can anyone advise how this can be done?
My View controller class ...
import UIKit
class CreateSessionViewController : UIViewController, UIPickerViewDataSource,UIPickerViewDelegate {
//Outlets:
@IBOutlet var durationPicker: UIPickerView!
@IBOutlet var typePicker: UIPickerView!
//Dummy duration array:
var durationArray = ["10", "20", "30", "40", "50", "60"];
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.durationPicker.dataSource = self;
self.durationPicker.delegate = self;
}
//Picker view methods:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return durationArray.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return durationArray[row]
}
}
Now, durationPicker is working great, but how would I populate typePicker?
You need to filter in the delegate method like this
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
if durationPicker == pickerView {
return 1 // The amount that you need for the durationPicker
} else if typePicker == pickerView {
return 2 // The amount that you need for the typePicker
}
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if durationPicker == pickerView {
return durationArray.count
} else if typePicker == pickerView {
return typeArray.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if durationPicker == pickerView {
return durationArray[row]
} else if typePicker == pickerView {
return typeArray[row]
}
}
Hope this help you!