简体   繁体   中英

UIPICKERVIEW with different data to be displayed according to the row selected

I have attached my code for your reference. I can able to display the data in Pickerview as i change the row the data changes accordingly but the data which selected from the picker is not getting displayed in the UITextfield . if I select any option in Pickerview I could not see any text inside the uitextfield if I select it still showing blank text field.

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

    @IBOutlet weak var textfield1: UITextField!

    @IBOutlet weak var textfield2: UITextField!



    var x = 0

    let leaguepickerview_destination = UIPickerView()
    let clubpickerview_denomination = UIPickerView()

    let league_destination = ["India","Europe","Australia","New Zealand","USA & Rest of the World"]
    let club_denomination = [[" Rs.500","Rs.1000","Rs.1500","Rs.2000","Rs.2500","Rs.3000","Rs.3500","Rs.4000","Rs.4500","Rs.5000"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"]]

    override func viewDidLoad() {
        super.viewDidLoad()

        leaguepickerview_destination.delegate = self
        leaguepickerview_destination.dataSource = self

        clubpickerview_denomination.delegate = self
        clubpickerview_denomination.dataSource = self

        textfield1.inputView = leaguepickerview_destination
        textfield2.inputView = clubpickerview_denomination


        textfield1.delegate = self
        textfield2.delegate = self


    }

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

        return 1

    }

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

        switch (pickerView){
        case leaguepickerview_destination:
            return league_destination.count
        case clubpickerview_denomination:
            return club_denomination[x].count
        default:
            return 0
        }
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch (pickerView){
        case leaguepickerview_destination:
            return league_destination[row]
        case clubpickerview_denomination:
            return club_denomination[x][row]
        default:
            return "an error occurred"
        }
    }

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

        if (pickerView == leaguepickerview_destination) {
            x = row
            clubpickerview_denomination.reloadAllComponents()
        }

    }

}

You need to set the textfield text

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

    if (pickerView == leaguepickerview_destination) {
       x = row
       clubpickerview_denomination.reloadAllComponents()
       textfield1.text = league_destination[row]
    }
    else {
       textfield2.text =  club_denomination[x][row]
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM