1

Currently, I got my code to work correctly, but the problem I'm having issue with the UIPickerView. Right now, If I select Cameron brand, it goes to var Cameron but if i select Shaffer brand, it still goes to var Cameron.

How can I rewrite the code so when I pick a different brand, it select a different array?

For example, if I select brand cameron, it select cameron, if i select brand Shaffer, it pick shaffer, etc.

class Picker: UIViewController, UIPickerViewDelegate, UITextFieldDelegate

{
    var activeTextField:UITextField?

    @IBOutlet var pickerView1: UIPickerView!
    @IBOutlet var pickerView2: UIPickerView!
    @IBOutlet var pickerView3: UIPickerView!

    @IBOutlet var textField1: UITextField!
    @IBOutlet var textField2: UITextField!
    @IBOutlet var textField3: UITextField!

    var brand = ["Cameron","Shaffer", "Hydril"]
    var cameron = ["D Annular Preventer", "UM Ram Preventer", "U Ram Preventer"]
    var shaffer = ["Spherical Annular Preventer", "LXT Ram Preventer", "NXT Ram Preventer"]
    var size = ["7 1/16","11","13 5/8"]
    var size2 = ["8 5/8","12","15 5/8"]

    override func viewDidLoad() {

        super.viewDidLoad()

        pickerView1 = UIPickerView()
        pickerView2 = UIPickerView()
        pickerView3 = UIPickerView()

        pickerView1.tag = 0
        pickerView2.tag = 1
        pickerView3.tag = 2 

    }

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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        if pickerView.tag == 0 {
            return brand.count
        } else if pickerView.tag == 1 {
            return cameron.count
        } else if pickerView.tag == 2 {
            return size.count
        }
        return 1
    }

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

        if pickerView.tag == 0 {
            return brand[row]
        } else if pickerView.tag == 1 {
            return cameron[row]
        } else if pickerView.tag == 2 {
            return size[row]
        }
        return ""
    }

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

        if pickerView.tag == 0 {
            textField1.text = brand[row]
        } else if pickerView.tag == 1 {
            textField2.text = cameron[row]
        } else if pickerView.tag == 2 {
            textField3.text = size[row]
        } 
    }
2
  • Lacking the energy to type out the answer, are you able to understand / use Dictionaries instead of hardcoded array literals here, so you can associate each brand with its allowable values? That way the allowable-value-array can be looked up based on using the brand as a key. Commented Mar 8, 2015 at 23:01
  • I haven't used dictionaries before. It's something I may need to look at. Commented Mar 8, 2015 at 23:04

1 Answer 1

2

cameron has to be replaced with a dynamic lookup. You'll need a dictionary of brands, which you can then tie to the UI:

[As I was writing this up I found the variable names awful; please avoid 0, 1, 2, insanity of numbering your variables, 3, 4, bleah]

At class level:

var brandLookup = [ "cameron": [ "x", "y", "z"], "anotherBrand": ... ]

then your delegate functions:

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView.tag == 0 {
        return brand.count
    } else if pickerView.tag == 1 {
        let brand = textField1.text
        let modelsForBrand = brandLookup[brand]!
        return modelsForBrand.count
    }
    ...
}

similarly:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {
   if pickerView.tag == 0 {
        textField1.text = brand[row]
        pickerView2.reloadAllComponents() // this picker has new data now
   } else if pickerView.tag == 1 {
       let brand = textField1.text
       let modelsForBrand = brandLookup[brand]!
       textField2.text = modelsForBrand[row]
   }
...
}

Finally:

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    // left as an exercise for you
}
Sign up to request clarification or add additional context in comments.

3 Comments

So, I'm assuming to replace the picker view code ' func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { if pickerView.tag == 0 { return brand.count } else if pickerView.tag == 1 { let brand = textField1.text let modelsForBrand = brandLookup[brand]! return modelsForBrand[row] return 1 } '
I got the coding to work with the exception of the code "return modelsForBrand[row].count"..it getting me the "Use of unresolved identifier 'row' ".......if i put in return 3...it works...thanks.
Fixed... count is a property of an array, and I had row out of context. Avoid hardcoded values! Turns your code into unstable garbage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.