如何让pickerView变得动态?

时间:2015-06-10 22:51:08

标签: ios swift uipickerview

在第一个屏幕中,用户在第二个屏幕中输入与阵列的索引相关的一些数字。我通过基本的prepareforsegue覆盖传输索引整数。但是,现在我被困在如何为第二个viewcontroller定义类,所以我可以使用相关的数组作为viewPickerDataSource。

例如,我认为这样可行:

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!
}

正如你可以从代码中推断的那样,我基本上有一个0-2传递给当前的ViewController。我定义了一个多维数组,希望如果我选择pickerDataSrouce [Index],它会为我创建合适的pickerView源代码。但是,脚本在加载屏幕之前崩溃,调试直接进入pickerDataSource [secondDataPassed]。有更好的方法吗?

0 个答案:

没有答案