4

Using Swift in Xcode, I want to make:

1) A PICKER, with data from an array

2) A BUTTON, when pressed will update a LABEL with the text from a selected row of the PICKER

My code currently:

var array = ["Sydney", "London", "Washington", "Tokyo", "San Francisco"]

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

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return array.count
}

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

@IBOutlet weak var PICKER: UIPickerView!

@IBOutlet weak var LABEL: UILabel!

@IBAction func BUTTON(sender: AnyObject) {
    LABEL.text = array[PICKER.selectedRowInComponent(0)]
}

So far everything worked. But I have 2 questions:

1) Should I put a "0" in PICKER.selectedRowInComponent? What does it mean? Because it didn't work without a number or with any other number. Wouldn't that mean selecting only the first row (instead of selecting the row the user has selected)?

2) How do I make the PICKER to show the middle item of an array by default and not the first item when the app loads (e.g. Washington in this case)?

Allister Bah
  • 1,134
  • 3
  • 13
  • 19
  • You might find an answer for your question here: http://stackoverflow.com/questions/812995/select-row-after-uipickerview-is-loaded – Artrmz May 14 '15 at 12:38

2 Answers2

5

Pickers can have multiple wheels (components). For example, you might have separate wheels for each digit, or you might have day, month, and year wheels. They are numbered starting at 0. So "Component 0" is just the first wheel (and you only have one).

Selecting rows is done by calling selectRow(inComponent:animated:).

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Thank you! Regarding 2) How best is it to jump straight to a middle item instead of counting the number of items in the array? – Allister Bah May 15 '15 at 03:40
  • I don't know what you mean "instead of counting the number of items." If you want it to "jump" just pass `animated:false`. This is starting to track off of the original question, though. If you have further questions, you should create a new question so future users can search for the answer. – Rob Napier May 15 '15 at 13:13
  • I do love some SDK docs. Who thinks "A zero-indexed number identifying a component." is helpful? Thanks @RobNapier – Pat Long - Munkii Yebee Mar 26 '18 at 10:33
4

UIPickerView can have multiple wheels (named components), and are numbered starting at 0. For example, a picker view with 3 components looks like the following 3 components picker where

  • the first wheel ("6") is component 0
  • the second wheel ("00") is the component 1
  • the third wheel ("AM") is the component 2

Unrelated to question title, but selecting a row is done by calling selectRow(_:inComponent:animated:)

var array = ["Sydney", "London", "Washington", "Tokyo", "San Francisco"]

// code...

yourPicker.selectRow(array.count/2, inComponent: 0, animated: true);
vladdeSV
  • 55
  • 1
  • 9
Dũng Nguyễn
  • 103
  • 1
  • 3
  • Thank you! Regarding 2) How best is it to jump straight to a middle item instead of counting the number of items in the array? – Allister Bah May 15 '15 at 03:41
  • store a variable for the row every time the didSelectRow delegate method is called. Therefor, when the pickerView is called to show again, set the selectRow to the store variable of the row. – cmario Jan 25 '16 at 09:09