UIPICKERVIEW,根据选择的行显示不同的数据

时间:2018-06-28 09:12:31

标签: ios swift macos uipickerview

我已附上我的代码供您参考。我可以在Pickerview中显示数据,因为我更改了数据相应更改的行,但是从Picker中选择的数据未显示在UITextfield中。如果我在Pickerview中选择了任何选项,如果我选择uitextfield时仍显示空白文本字段,则看不到任何文本。

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()
        }

    }

}

1 个答案:

答案 0 :(得分:0)

您需要设置文本字段文本

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]
    }
}