如何在UITextfield中显示UIPickerview数据

时间:2019-05-28 04:01:56

标签: ios swift uipickerview

我有7个UIPickerviews和7个UITextfields。我希望每个UIPickerview数组的总和显示在分配的UITextfield中。

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if parScore.tag == 0 {
        return parArray.count
    } else if player1Score.tag == 1 {
        return player1Array.count
    } else if Picker2.tag == 2 {
        return player2Array.count
    } else if player3Score.tag == 3 {
        return player3Array.count
    } else if player4Score.tag == 4 {
        return player4Array.count
    } else if player5Score.tag == 5 {
        return player5Array.count
    } else if player6Score.tag == 6 {
        return player6Array.count
    }
    return 1
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if parScore.tag == 0 {
        return pars[row]
    } else if player1Score.tag == 1 {
        return player1[row]
    } else if Picker2.tag == 2 {
        return player2[row]
    } else if player3Score.tag == 3 {
        return player3[row]
    } else if player4Score.tag == 4 {
        return player4[row]
    } else if player5Score.tag == 5 {
        return player5[row]
    } else if player6Score.tag == 6 {
        return player6[row]
    }

    return ""
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if parScore.tag == 0 {
        self.parScore.text = pars[row]
       self.parScores = pars[row]
    } else if player1Score.tag == 1 {
        self.player1Score.text = player1[row]
       self.player1Scores = player1[row]
    } else if Picker2.tag == 2 {
        self.player2Score.text = player2[row]
        self.player2Scores = player2[row]
    } else if player3Score.tag == 3 {
        self.player3Score.text = player3[row]
        self.player3Scores = player3[row]
    } else if player4Score.tag == 4 {
        self.player4Score.text = player4[row]
        self.player4Scores = player4[row]
    } else if player5Score.tag == 5 {
        self.player5Score.text = player5[row]
        self.player5Scores = player5[row]
    } else if player6Score.tag == 6 {
        self.player6Score.text = player6[row]
        self.player6Scores = player6[row]
    }
}

@IBAction func nextButtonPressed(_ sender: Any) {
     if firstIndex < 18 {
         updateBallImages()
         updatePar()
         updateP1()
         updateP2()
         updateP3()
     } else if firstIndex == 18 {
        repeatProg()
     }
}

func updateBallImages(){
    firstIndex = Int((firstIndex+1))
    ball1.image = UIImage(named: ballArray[firstIndex])
}

func repeatProg(){
    firstIndex = Int((firstIndex+1))
    ball1.image = UIImage(named: ballArray[firstIndex])
}

func updatePar(){
    par = Int(par)
    parScore.text = String(par)
}

func updateP1(){
    p1 = Int(p1)
    player1Score.text = String(p1)
}

func updateP2(){
    p2 = Int(p2)
    player2Score.text = String(p2)
    player2Score.inputView = self.Picker2
}

func updateP3(){
    p3 = Int(p3)
    player3Score.text = String(p3)
}

每次尝试按下UIButton时,我都试图将UIPickerview的值添加到UITextfield中。

0 个答案:

没有答案