Get values from a textfield array

前端 未结 4 491
难免孤独
难免孤独 2021-01-29 10:07

I would like to access the values of my textfield array but confused as the array I created is with tags. So anyone knows how to get the value of the list (array) I created?

4条回答
  •  死守一世寂寞
    2021-01-29 10:19

    Let's say you have following array,

        var txtArray:[UITextField] = [UITextField]()
    
        for i in 0...4 {
            let txtField = UITextField(frame: .zero)
            txtField.text = "\(i)"
            txtField.tag = i
            txtArray.append(txtField)
        }
    

    To get values you have to do following,

        let sorted = txtArray.sorted { $0.tag < $1.tag }
        let values = sorted.map { return $0.text! }
        let test = values.joined(separator: " ")
    
        print(test)
    

    Output will be

    0 1 2 3 4
    

提交回复
热议问题