How do I allow my pickerView to be dynamic?

356 views Asked by At

From the first screen, the user inputs some numbers pertaining to an Index for an array in the second screen. I transfer the index integer through a basic prepareforsegue override. However, now I am stuck at how I can define the class for the second viewcontroller so I can use the relevant array as the viewPickerDataSource.

For example, I thought this would work:

class ChooseColorViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

var pickerDataSource: [[String]] = [['orange', 'yellow' 'red'], ['blue', 'cyan', 'sky blue'], ['red', 'magenta','purple']]

override func viewDidLoad() {
    super.viewDidLoad()
    self.modelViewPicker.dataSource = self;
    self.modelViewPicker.delegate = self;

    testLabel.text = DataPassed
    println(DataPassed)
    //This tests to see if the index got transferred over... it did indeed.

    // Do any additional setup after loading the view.
}

@IBOutlet weak var testLabel: UILabel!
var DataPassed:Int!

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerDataSource[DataPassed].count;
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return pickerDataSource[DataPassed][row]
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBOutlet weak var modelViewPicker: UIPickerView!
}

As you can infer from the code, I essentially have an integer 0-2 passed into the current ViewController. I defined a multi-dimensionaly array hoping that if I do pickerDataSrouce[Index], it would create the appropriate pickerView source for me. However, the script crashes before the screen loads, and the debug goes straight to the pickerDataSource[secondDataPassed]. Is there a better way to do this?

0

There are 0 answers