Stuck Beginner
Stuck Beginner

Reputation: 93

Using the value on 1st component on UIPickerView to change the remaining components

just got some great help which made the UIPickerView wheels work perfectly but they I have been trying to get the data in the 2nd and 3rd components to change dependant on the position of the 1st component.

I can get through some println lines to work out that the variable that I pull out whatConversion has the correct value but I have no idea how to change the array and make the UIPickerView update with the new values.

Please help for my sanity and also I am going to have to put extra time in at work after spending nearly all day on these, what dozen lines of code.

Thanks in advance

Motty

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate {

@IBOutlet weak var picker1Label: UILabel!
@IBOutlet weak var picker2Label: UILabel!

@IBOutlet weak var bigPicker: UIPickerView!

var wheelContents:[[String]] = []
var length = ["metres","feet","yards","inches","mm","cm","miles"]
var volume = ["m3","US Gall","Imp Gall","Barrels", "cubic FT","litres"]
var conType = ["length","volume"]


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    bigPicker.delegate = self
    wheelContents = [conType, length, length]

}

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


//For main selection of type of conversion
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(bigPicker: UIPickerView) -> Int{

    return wheelContents.count

}

// returns the # of rows in each component..
func pickerView(bigPicker: UIPickerView, numberOfRowsInComponent component: Int) -> Int{

    return wheelContents[component].count

}

func pickerView(bigPicker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{

    return wheelContents[component][row]

}

func pickerView(bigPicker: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    var whatConversion = wheelContents[0][bigPicker.selectedRowInComponent(0)]

    switch(whatConversion){

        case "length":
        wheelContents = [conType, length, length]
        bigPicker.numberOfRowsInComponent(wheelContents[component].count)

        break



        case "volume":
        wheelContents = [conType, volume, volume]

        break

    default:
        break

    }

}




}

Upvotes: 0

Views: 905

Answers (1)

Stuck Beginner
Stuck Beginner

Reputation: 93

I am even happier I managed to figure this out myself by looking at the Class definitions and functions

used bigPicker.reloadAllComponents()

Upvotes: 2

Related Questions