1

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)
}

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

For the moment everything works correctly with UIScrollView. Perhaps I will try to create version with UITableViewCell too.
So everything works? You created successfully an Array of UITextFields?
Yes, almost everything was written correctly. I made only a simple mistake in declaration of an UITextFields Array. Thanks for your help!
Accepted! I also voted up, unfortunately I have less than 15 reputation.
1

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.

Comments

0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.