Alex Tarkowski Alex Tarkowski - 1 month ago 5
iOS Question

Swift xcode dynamically adding text inputs in view

I am relatively new to swift and have been used to a JavaScript environment so correct me if it is not possible, but is there a way to dynamically add rows of text inputs and thus data in the form of a nested dictionary, from the view via a button?

I am looking to create an object with no set constraints on the limitations of data.

Essentially the object would be structured as:

"Object1": ["row1":[desc: "desc1", option: "option1"]]


and when a user adds a row through a UIButton:

"Object1": ["row1":[desc: "desc1", option: "option1"], "row2": [desc: "desc2", option: "option2"]]


and so on....

Image example of what I am looking to achieve

Thank you for any input provided.

Answer

With the feedback of Will M I did overthink this again and came up with another solution:

class ViewController: UIViewController {

    let xPos : CGFloat = 0
    var yPos : CGFloat = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func press(sender: AnyObject) {

        yPos += 22
        let tf = UITextField()
        tf.frame = CGRectMake(xPos, yPos, 100, 20)
        tf.backgroundColor = UIColor.blackColor()
        self.view.addSubview(tf)

    }

}

Here is a screenshot of what the result looks like I hope this helps you!

http://imgur.com/m59xDfN

The black things are textfields

Comments