Search code examples
iosswiftuipickerview

Change Label According to UIPickerView's Selected Row in If Statement


I've meticulously searched through multiple pages and posts but couldn't quite get exactly what I need. I am currently working on a project and one of the controllers has several text fields with outputs using a single UIPickerView, and sometimes the labels need to change depending on the user's selection.

How would I change a label according to a UIPickerView's selected row, especially in an if statement?

I made a simple test code and example to demonstrate.

Code

class TestViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var categoryOutput: UITextField!
@IBOutlet weak var shapeOutput: UITextField!
@IBOutlet weak var lengthDiaOutput: UITextField!
@IBOutlet weak var heightOutput: UITextField!

@IBOutlet weak var dimensionLabel: UILabel!

let category = ["Perimeter", "Area"]
let shape = ["Rectangle", "Circle", "Triangle"]


// MARK: - viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()

    createCategoryPicker()
    createShapePicker()
}


// MARK: - UIPickerViews

// Create a UIPickerView as an input text field for 'Category'.

func createCategoryPicker() {

    let categoryPicker = UIPickerView()
    categoryPicker.delegate = self
    categoryPicker.tag = 1

    categoryOutput.inputView = categoryPicker

    categoryPicker.backgroundColor  = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1.0)
}

// Create a UIPickerView as an input text field for 'Shape'.

func createShapePicker() {
    let shapePicker = UIPickerView()
    shapePicker.delegate = self
    shapePicker.tag = 2

    shapeOutput.inputView = shapePicker

    shapePicker.backgroundColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1.0) 
    }

}


// MARK: - Extensions

// Create a UIPickerView.

extension TestViewController: UIPickerViewDelegate, UIPickerViewDataSource {

// Set number of lists.

func numberOfComponents(in pickerView: UIPickerView) -> Int {

    return 1
}

// Set number of rows in list.

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

    if pickerView.tag == 1 {
        return category.count }
    if pickerView.tag == 2 {
        return shape.count }
    return 0
}

// Set content of list.

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

    if pickerView.tag == 1 {
        return category[row] }
    if pickerView.tag == 2 {
        return shape[row] }
    return nil
}

// Grab selection and change label.

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

    if pickerView.tag == 1 {
        categoryOutput.text = category[row] }
    if pickerView.tag == 2 {
        shapeOutput.text = shape[row]

        if component == 1 {
            dimensionLabel.text = "Diameter" }
        else {
            dimensionLabel.text = "Length" }
     }
}

// Set appearance of contents.

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

    var label: UILabel

    if let view = view as? UILabel {
        label = view } 
    else { label = UILabel() }

    if pickerView.tag == 1 {
        label.text = category[row] }
    if pickerView.tag == 2 {
        label.text = shape[row] }

    label.font = UIFont(name: "Avenir", size: 17)
    label.textColor = .black
    label.textAlignment = .center

    return label
    }
}

This is the section that needs focusing.

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

    if pickerView.tag == 1 {
        categoryOutput.text = category[row] }
    if pickerView.tag == 2 {
        shapeOutput.text = shape[row]

        if component == 1 {
            dimensionLabel.text = "Diameter" }
        else {
            dimensionLabel.text = "Length" }
     }
}

Example

enter image description here

The text label, Dimension, will not change to Diameter when Circle is selected.


Solution

  • func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
        if pickerView.tag == 1 {
            categoryOutput.text = category[row] }
        if pickerView.tag == 2 {
            shapeOutput.text = shape[row]
    
            if row == 1 //Circle{
                dimensionLabel.text = "Diameter" }
            else {
                dimensionLabel.text = "Length" }
         }
    }