0

In my current application, I have a UITableViewController that allows me to segue to three different indices depending on the row selected. If I select row one, I segue to a view controller with three UITextFields. If I select row two, I segue to a NEW view controller with one UITextField. If I select row three, I segue to ANOTHER NEW view controller with five UITextFields. I am looking for a way to condensing the view controllers I am going to into one view controller that will dynamically hide/unhide or remove/add the needed number of UITextFields depending on the index selected in the table view controller. I need the text fields to display in a vertical stack view.

Although it would be very valuable, I am not looking for a solution/example from someone, but rather letting me know some topics I can research would be extremely helpful.

Thank you for your time,

Tony

Tony Pendleton
  • 358
  • 1
  • 11

1 Answers1

0

There might be different approaches for this problem, but I would create a stackView in the NEW view controller with the maximum possible number of textFields embedded. Lets say the VC with most textFields can contain 3 textFields. Create your stackView with 3 textFields inside.

Then segue the indexPath.row of tableView to the NEW VC, it can hold a variable of Integer like:

var index: Int?

In didSelectRowAt method of tableView:

performSegue(withIdentifier: "yourIdentifier", sender: indexPath.row)

In prepareForSegue method:

if segue.identifier == "yourIdentifier"
let vc = segue.destination as! NEWViewController
vc.index = sender

Set tag to your textFields in your NEW View controller, so you can know which textField you will want to delete depending on index. Then in viewDidLoad of your newViewController, you can remove textFields from your stackView according to the index (Then you might run into which ):

    var counter = 0
    while counter < index {
        answerStackView.subviews.forEach { (view) in
            if counter < index {
                if view is UITextField {
                    if view.tag == /*something here to delete specific textFields depending on index. */ {
                               view.removeFromSuperview()
                    }
                }
            }

            counter += 1
        }
    }
emrepun
  • 2,496
  • 2
  • 15
  • 33