user5006803
user5006803

Reputation:

How to create an array of UITextField objects in Swift?

I am working on an application in Swift. At the beginning I provide number of inputs that I would like to add. In next view I create as many UITextFields as I defined in first view.I have a problem with storing of all this values from TextFields after pressing the button. It seems to me that the most effective solution is to create an array of UITextFields and add every value of TextField into array and then in action for button adding values which are in all TextFields into array. Unfortunately, it doesn't work. Any suggestions? Thanks in advance.

override func viewDidLoad() {
    super.viewDidLoad()

    var counter =  0
    for i in 1...mainInstance.numberOfInputs
    { 

    super.viewDidLoad()       
    array.insert(UITextField(frame: CGRect(x: 0, y: 50, width: 60, height: 20)), at: i)
    array[i].center = CGPoint(x: 200,y: 50 + counter)
    array[i].textAlignment = NSTextAlignment.center
    array[i].layer.borderWidth = 1
    array[i].layer.borderColor = UIColor.black.cgColor
    self.outletScrollView.addSubview(array[i])
    counter += 50

    }

    super.viewDidLoad()
    let button = UIButton(type: .system) // let preferred over var here
    button.frame = CGRect(x: 200, y: 50 + counter, width: 100, height: 20)
    button.setTitle("Recalculate", for: UIControlState.normal)
    button.addTarget(self, action: "actionRecalculate:", for: UIControlEvents.touchUpInside)
    self.outletScrollView.addSubview(button)
    outletScrollView.contentInset = UIEdgeInsetsMake(0, 0, CGFloat(counter+100), 0)
}

Upvotes: 1

Views: 6640

Answers (3)

10donovanr
10donovanr

Reputation: 93

Though this might not be for the exact use case, it could help others with similar use cases (particularly programmatically created textfields or custom uicell).

txtfield.tag = indexPath.row // or other int

Then you implement textfield didEndEditing after array has been initialized and delegating

@IBAction func endedEditing(_ sender: UITextField) {
    arr[sender.tag] = sender.text
}

Upvotes: 0

syncster31
syncster31

Reputation: 66

Creating array of UITextfield could be done this way

// Create an empty array of type UITextField
var listOfTextFields : [UITextField] = []

// In viewDidLoad, create a textfield and add it to the array
override func viewDidLoad() {
    super.viewDidLoad()

    for i in 0...mainInstance.numberOfInputs-1 {
        let textField = UITextField((frame: CGRect(x: X, y: Y*i, width: yourWidth, height: yourHeight)))
        textField.delegate = self
        listOfTextFields.append(textField)
    }
}

In this UITextField setup, I didn't account for any formatting to shorten the answer.

Upvotes: 1

derdida
derdida

Reputation: 14904

You could create easily with:

let textFields = [UITextField]()

You should consider using an UIScrollView, (like in your example) or (better idea in my opinon) a UITableView to add your UITextfields. So you have multiple Rows, and every row has one UITextField. The number of rows / Textfield can u easily customize (and get it from your FirstViewController for example).

Just create a custom UITableViewCell - add an UITextField and connect the IBOutlet. To get the values from every UITextField you could iterate through all cells, and read out the values. Just ask, if you need more help about doing that.

Upvotes: 1

Related Questions