I have 2 UIPickerController's in one of View Controllers. I can get one to work, but when I add a second, my app crashes. Here is the code I use for one picker view:
import UIKit
class RegisterJobPosition: UIViewController, UIPickerViewDelegate{
@IBOutlet weak var positionLabel: UILabel!
var position = ["Lifeguard", "Instructor", "Supervisor"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(PickerView: UIPickerView!) -> Int
{
return 1
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int
{
return position.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!
{
return position[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
positionLabel.text = position[row]
}
}
Now, how can I get a second controller to work? Say my second PickerView is called location
(the other one is called position
). I tried duplicating the code within the PickerView methods for location
but it doesn't work.
I'm using Swift.
I think the biggest issue and different to Java is that Java easily allow for attributes to be passed through the constructor. e.g. you could declare class LocationDataSourceDelegate as generic and call it genericDataSourceDelegate and make the constructor accept and Array public genericDataSourceDelegate (String data[]) and be able to make one class where could just simply create objects of. You just instantiate it and pass location the constructor like genericDataSourceDelegate (location)
The problem with your model you will have to create as many delegate classes in one program which is a strain to your compiler.
I found this to work.
Here is my solution: - on the Storyboard, add 2 PickerView to your View - set the 1st picker´s tag as #1 & #2 for the 2nd picker under the Attributes Inspector - CTRL + drag from each picker to the top yellow View Controller icon and choose dataSource.Repeate the same choosing delegate - repeate the above for the other picker too - add pickerview & pickerviewdelegation to your ViewController class:
in your ViewController class, create empty arrays for the pickers:
on viewDidLoad() method, populate the arrays with your content:
implement the delegate & pickerview methods:
Enjoy, have fun and express your happiness with a positiveness sign.
My background is in Android but my answer is very OOP. I would suggest creating different classes to implement the DataSource and Delegate like this:
and then another one for the Location:
then in your RegisterJobPosition you need to create an instance of each:
and assign them to the pickers like this:
and you can access the selected position and location using:
Hope this helps you and others and I'm also hoping for some constructive comments of why this is not "swifty"
Based on the information I have in the question, I'd say that you need to set up the data source & delegate methods to handle the ability to distinguish between which picker instance is calling them.
Using the tag property on the picker view is one strategy.
There should be some if/else or switch statements in the methods that have varying logic depending on whether it's the location or the position picker that's being referenced.