更改另一个UIPickerView后更改UIPickerView的内容

时间:2015-12-02 16:00:15

标签: ios swift uitextfield uipickerview

我有一个包含许多PickerView的表单,当在另一个PickerView上选择内容时会给出一个内容,因此当我们更改值时,必须更改该行。我的UIPickerView就是这样的:

import UIKit

@objc
protocol DemandeGratuiteViewControllerDelegate {
    optional func toggleLeftPanel()
    optional func collapseSidePanels()
}

class DemandeGratuiteViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource , UITextFieldDelegate {

    var delegate: DemandeGratuiteViewControllerDelegate?

    @IBOutlet var tfProjet: UITextField!
    @IBOutlet var tfLogement: UITextField!
    @IBOutlet var libelleValeurActuelleDuBien: UILabel!
    @IBOutlet var libelleLoyer: UILabel!
    @IBOutlet var tfValeurActuelleBien: UITextField!
    @IBOutlet var tfLoyer: UITextField!
    @IBOutlet var tfProfession: UITextField!
    @IBOutlet var tfContrat: UITextField!
    @IBOutlet var pickerView: UIPickerView!
    @IBOutlet weak var RevenusNetsConstraint: NSLayoutConstraint!
    @IBOutlet weak var libelleDeclare: UILabel!

    var pvProjetData = ["-Choisir-", "Rachat de crédits", "Renégociation de crédits"]
    var pvLogement = ["-Choisir-", "Propriétaire", "Locataire", "Logement de fonction", "Hébérgé par un tiers", "Hébérgé poar la famille"]
    var pvProfession = ["-Choisir-", "Agriculteur exploitant", "Artisan, commercant", "Cadre", "Chef d'entreprise", "Fonctionnaire", "Intérimaire", "Ouvrier", "Retraité", "Salarié", "Sans emploi"]
    var pvContrat1 = ["Profession libérale"]
    var pvContrat2 = ["-Choisir-", "CDI", "CDD", "Stagiaire"]
    var pvContrat3 = ["-Choisir-", "Stagiaire", "Titulaire", "Contractuelle"]
    var pvContrat4 = ["Intérimaire"]
    var pvContrat5 = ["Retraité"]
    var pvContrat6 = ["Sans contrat"]
    var pvFichage = ["-Choisir-", "Non", "Interdit bancaire", "FICP", "Les deux"]
    var pvDureeRemb12 = ["-Choisir-", "5", "6", "7", "8", "9", "10", "11", "12"]
    var pvDureeRemb25 = ["-Choisir", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"]
    var pvSituationFamille = ["-Choisir", "Marié(e)", "Célibataire", "Veuf(ve)", "Divorcé(e)", "Pacsé(e)", "Union libre", "En instance de divorce"]
    var pvNombreEnfants = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
    var pvCivilite = ["--", "M", "Mme", "Mlle"]


    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 38.0/255.0, green: 51.0/255.0, blue: 85.0/255.0, alpha: 1.0)
        self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Gotham", size: 13)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
        self.title = "DEMANDE GRATUITE"
        // Do any additional setup after loading the view, typically from a nib.
        //        pickerView.tag = 1
        //        pickerView.delegate = self
        let pickerProjetData = UIPickerView()
        let pickerLogement = UIPickerView()
        let pickerProfession = UIPickerView()
        let pickerContrat1 = UIPickerView()
        let pickerContrat2 = UIPickerView()
        let pickerContrat3 = UIPickerView()
        let pickerContrat4 = UIPickerView()
        let pickerContrat5 = UIPickerView()
        let pickerContrat6 = UIPickerView()
        let pickerDureeRemb12 = UIPickerView()
        let pickerDureeRemb25 = UIPickerView()
        let pickerSituationFamille = UIPickerView()
        let pickerNombreEnfants = UIPickerView()
        let pickerCivilite = UIPickerView()

        pickerProjetData.tag = 0
        pickerLogement.tag = 1
        pickerProfession.tag = 2
        pickerContrat1.tag = 3
        pickerContrat2.tag = 4
        pickerContrat3.tag = 5
        pickerContrat4.tag = 6
        pickerContrat5.tag = 7
        pickerContrat6.tag = 8
        pickerDureeRemb12.tag = 9
        pickerDureeRemb25.tag = 10
        pickerSituationFamille.tag = 11
        pickerNombreEnfants.tag = 12
        pickerCivilite.tag = 13

        pickerProjetData.delegate   = self
        pickerLogement.delegate   = self
        pickerProfession.delegate = self
        pickerContrat1.delegate = self
        pickerContrat2.delegate = self
        pickerContrat3.delegate = self
        pickerContrat4.delegate = self
        pickerContrat5.delegate = self
        pickerContrat6.delegate = self
        pickerDureeRemb12.delegate = self
        pickerDureeRemb25.delegate = self
        pickerSituationFamille.delegate = self
        pickerNombreEnfants.delegate = self
        pickerCivilite.delegate = self

        pickerProjetData.dataSource = self
        pickerLogement.dataSource = self
        pickerProfession.dataSource = self
        pickerContrat1.dataSource = self
        pickerContrat2.dataSource = self
        pickerContrat3.dataSource = self
        pickerContrat4.dataSource = self
        pickerContrat5.dataSource = self
        pickerContrat6.dataSource = self
        pickerDureeRemb12.dataSource = self
        pickerDureeRemb25.dataSource = self
        pickerSituationFamille.dataSource = self
        pickerNombreEnfants.dataSource = self
        pickerCivilite.dataSource = self

        self.tfProjet.inputView = pickerProjetData
        self.tfLogement.inputView = pickerLogement
        self.tfProfession.inputView = pickerProfession


        tfLogement.delegate = self
        tfLogement.addTarget(self, action: "tfLogementDidChange:", forControlEvents: .EditingChanged)

        RevenusNetsConstraint.priority = 999
        libelleLoyer.hidden = true
        libelleValeurActuelleDuBien.hidden = true
        tfLoyer.hidden = true
        tfValeurActuelleBien.hidden = true


        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))
    }

    func tfLogementDidChange(){
        if(tfLogement.text == "Propriétaire"){
            RevenusNetsConstraint.priority = 994
            libelleValeurActuelleDuBien.hidden = false
            tfValeurActuelleBien.hidden = false
            tfLoyer.hidden = true
            libelleLoyer.hidden = true
        }
        else if (tfLogement.text == "Locataire"){
            RevenusNetsConstraint.priority = 994
            libelleValeurActuelleDuBien.hidden = true
            tfValeurActuelleBien.hidden = true
            tfLoyer.hidden = false
            libelleLoyer.hidden = false
        }
        else{
            RevenusNetsConstraint.priority = 999
            libelleLoyer.hidden = true
            libelleValeurActuelleDuBien.hidden = true
            tfLoyer.hidden = true
            tfValeurActuelleBien.hidden = true
        }
    }

    func handleTap(sender: UITapGestureRecognizer) {
        if sender.state == .Ended {
            view.endEditing(true)
        }
        sender.cancelsTouchesInView = false
    }


    @IBAction func menuTapped(sender: AnyObject) {
        print(delegate)
        delegate?.toggleLeftPanel?()
    }


    // returns the number of 'columns' to display.
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
        return 1
    }

    // returns the # of rows in each component..
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        if pickerView.tag == 0 {
            return pvProjetData.count
        }
        else if pickerView.tag == 1 {
            return pvLogement.count
        }
        else if pickerView.tag == 2 {
            return pvProfession.count
        }
        return 1
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView.tag == 0 {
            return pvProjetData[row]
        }
        else if pickerView.tag == 1 {
            return pvLogement[row]
        }
        else if pickerView.tag == 2 {
            return pvProfession[row]
        }
        return ""
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        if pickerView.tag == 0 {
            tfProjet.text = pvProjetData[row]
            self.tfProjet.endEditing(true)
        }
        else if pickerView.tag == 1 {
            tfLogement.text = pvLogement[row]
            self.tfLogement.endEditing(true)
            tfLogementDidChange()
            print(tfLogement.text)
        }
        else if pickerView.tag == 2 {
            tfProfession.text = pvProfession[row]
            self.tfProfession.endEditing(true)
        }
    }

}

所以我想用tfProfession中选择的内容更改tfContrat的UIPickerVie:

  • 如果选择了第一项,我会显示pvContrat1
  • 如果选择了第二项,我会显示pvContrat 2
  • 等...

0 个答案:

没有答案