从pickerview显示文本字段中的数组总数

时间:2019-06-10 00:26:09

标签: ios swift uipickerview

This is a screen shot of the app. 我正在尝试通过按next按钮添加最后一个选择器视图选择后,获取要在文本字段中显示的数组的总和。

这是我一直在努力的高尔夫得分应用程序,但是我是新手。可能是一团糟,但我一直在尝试几种不同的配置。

import UIKit
import Foundation

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    var ballArray : Array = ["ball1", "ball2", "ball3", "ball4", "ball5", "ball6", "ball7", "ball8", "ball9", "ball10", "ball11", "ball12", "ball13", "ball14", "ball15", "ball16", "ball17", "ball18","score"]
    var firstIndex : Int = 0


    var parScores = [Int]()
    var player1Scores = [Int]()
    var player2Scores = [Int]()
    var player3Scores = [Int]()
    var player4Scores = [Int]()
    var player5Scores = [Int]()
    var player6Scores = [Int]()

    var par : Int = 0
    var p1 : Int = 0
    var p2 : Int = 0
    var p3 : Int = 0
    var p4 : Int = 0
    var p5 : Int = 0
    var p6 : Int = 0


    var parArray  = [0,1,2,3,4,5,6,7,8,9,10]
    var player1Array = [0,1,2,3,4,5,6,7,8,9,10]
    var player2Array = [0,1,2,3,4,5,6,7,8,9,10]
    var player3Array = [0,1,2,3,4,5,6,7,8,9,10]
    var player4Array = [0,1,2,3,4,5,6,7,8,9,10]
    var player5Array = [0,1,2,3,4,5,6,7,8,9,10]
    var player6Array = [0,1,2,3,4,5,6,7,8,9,10]

    let pars = ["1","2","3","4","5","6","7","8","9","10","11"]
    let player1 = ["1","2","3","4","5","6","7","8","9","10","11"]
    let player2 = ["1","2","3","4","5","6","7","8","9","10","11"]
    let player3 = ["1","2","3","4","5","6","7","8","9","10","11"]
    let player4 = ["1","2","3","4","5","6","7","8","9","10","11"]
    let player5 = ["1","2","3","4","5","6","7","8","9","10","11"]
    let player6 = ["1","2","3","4","5","6","7","8","9","10","11"]



    @IBOutlet weak var ball1: UIImageView!

    @IBOutlet weak var PickerPar: UIPickerView!
    @IBOutlet weak var Picker1: UIPickerView!
    @IBOutlet weak var Picker2: UIPickerView!
    @IBOutlet weak var Picker3: UIPickerView!
    @IBOutlet weak var Picker4: UIPickerView!
    @IBOutlet weak var Picker5: UIPickerView!
    @IBOutlet weak var Picker6: UIPickerView!


    //    let par = UIPickerView()
    //    let p1 = UIPickerView()
    //    let p2 = UIPickerView()
    //    let p3 = UIPickerView()
    //    let p4 = UIPickerView()
    //    let p5 = UIPickerView()
    //    let p6 = UIPickerView()
    //


    @IBOutlet weak var parScore: UITextField!
    @IBOutlet weak var player1Score: UITextField!
    @IBOutlet weak var player2Score: UITextField!
    @IBOutlet weak var player3Score: UITextField!
    @IBOutlet weak var player4Score: UITextField!
    @IBOutlet weak var player5Score: UITextField!
    @IBOutlet weak var player6Score: UITextField!




    override func viewDidLoad() {
        super.viewDidLoad()

    //        var parSum = parScores.reduce(0, +)
    //        var player1Sum = player1Scores.reduce(0, +)
    //        var player2Sum = player2Scores.reduce(0, +)
    //        var player3Sum = player3Scores.reduce(0, +)
    //        var player4Sum = player4Scores.reduce(0, +)
    //        var player5Sum = player5Scores.reduce(0, +)
    //        var player6Sum = player6Scores.reduce(0, +)

        PickerPar.delegate = self
        PickerPar.dataSource = self
        PickerPar.tag = 0

        Picker1.delegate = self
        Picker1.dataSource = self
        Picker1.tag = 1

        Picker2.delegate = self
        Picker2.dataSource = self
        Picker2.tag = 2

        Picker3.delegate = self
        Picker3.dataSource = self
        Picker3.tag = 3

        Picker4.delegate = self
        Picker4.dataSource = self
        Picker4.tag = 4

        Picker5.delegate = self
        Picker5.dataSource = self
        Picker5.tag = 5

        Picker6.delegate = self
        Picker6.dataSource = self
        Picker6.tag = 6

        parScore.tag = 0
        player1Score.tag = 1
        player2Score.tag = 2
        player3Score.tag = 3
        player4Score.tag = 4
        player5Score.tag = 5
        player6Score.tag = 6



        }
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if PickerPar.tag == 0 {
            return parArray.count
        } else if Picker1.tag == 1 {
            return player1Array.count
        } else if Picker2.tag == 2 {
            return player2Array.count
        } else if Picker3.tag == 3 {
            return player3Array.count
        } else if Picker4.tag == 4 {
            return player4Array.count
        } else if Picker5.tag == 5 {
            return player5Array.count
        } else if Picker6.tag == 6 {
            return player6Array.count
        }
        return 1

    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if PickerPar.tag == 0 {
            return pars[row]
        } else if Picker1.tag == 1 {
            return player1[row]
        } else if Picker2.tag == 2 {
            return player2[row]
        } else if Picker3.tag == 3 {
            return player3[row]
        } else if Picker4.tag == 4 {
            return player4[row]
        } else if Picker5.tag == 5 {
            return player5[row]
        } else if Picker6.tag == 6 {
            return player6[row]
        }

        return ""

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

        if PickerPar.tag == 0 {
            self.parScore.text = pars[row]
        } else if Picker1.tag == 1 {
            self.player1Score.text = player1[row]
        } else if Picker2.tag == 2 {
            self.player2Score.text = player2[row]
        } else if Picker3.tag == 3 {
            self.player3Score.text = player3[row]
        } else if Picker4.tag == 4 {
            self.player4Score.text = player4[row]
        } else if Picker5.tag == 5 {
            self.player5Score.text = player5[row]
        } else if Picker6.tag == 6 {
            self.player6Score.text = player6[row]
        }

     }





    @IBAction func nextButtonPressed(_ sender: Any) {





         if firstIndex < 18 {
             updateBallImages()
             updatePar()
             updateP1()
             updateP2()
             updateP3()
             updateP4()
             updateP5()
             updateP6()
         }
            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(parArray[par])
    }
    func updateP1(){
        p1 = Int(p1)
        player1Score.text = String(player1Array[p1])
    }
    func updateP2(){
        p2 = Int(p2)
        player2Score.text = String(player2Array[p2])

    }
    func updateP3(){
       p3 = Int(p3)
        player3Score.text = String(player3Array[p3])
    }
    func updateP4(){
        p4 = Int(p4)
        player4Score.text = String(player4Array[p4])
    }
    func updateP5(){
        p5 = Int(p5)
        player5Score.text = String(player5Array[p5])
    }
    func updateP6(){
       print(p6 = player6Scores.reduce(0, +))
        player6Score.text = String(player6Array[p6])
    }
}

我要查找的输出将在其指定的文本字段中的每个pickerview中显示。每按一次按钮累积的总和。

0 个答案:

没有答案