Swift & UITextFieldDelegate: How to get object that called the delegate? Or call textbox with variable?
<p>I am trying to create my first app in XCode and Swift. Nothing major, just a simple calculator for a game I play often. Would be handy for me lol.</p>

<p>It simply is supposed to have 6 text boxes, each box is the number of materials I have. You can "combine" the materials. 4 level 1 materials combines to 1 level 2 material, and so on up to level 6.</p>

<p>I could have 1 level 1, 3 level 2, 1 level 3, 0 level 4, 3 level 5, 1 level 6.. I would put each one in a text box, hit the button, and it will tell me how many more level 1 materials I need to get another level 6.</p>

<p>Anyways, following the guide here,  <a href="https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson3.html" rel="nofollow">https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson3.html</a>, it has you manually defining the text box. However, I have 6, and I find it terrible to have to define each one manually. What if I had a huge app with 100 text boxes, ridiculous.</p>

<pre><code>import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    // MARK: Properties
    @IBOutlet weak var txt1: UITextField!
    @IBOutlet weak var txt2: UITextField!
    @IBOutlet weak var txt3: UITextField!
    @IBOutlet weak var txt4: UITextField!
    @IBOutlet weak var txt5: UITextField!
    @IBOutlet weak var txt6: UITextField!
    @IBOutlet weak var btnGo: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        txt1.delegate = self
        txt2.delegate = self
        txt3.delegate = self
        txt4.delegate = self
        txt5.delegate = self
        txt6.delegate = self
    }

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

    func textFieldShouldReturn(textField: UITextField) -&gt; Bool {
        // Hide the keyboard.
        textField.resignFirstResponder()
        return true
    }

    func textFieldDidEndEditing(textField: UITextField) {
        // here is where its dumb
        txt1.text = textField.text
    }

    // MARK: Actions

    @IBAction func calculateItems(sender: UIButton) {
        // do my calculations
    }


}
</code></pre>

<p>As you can see in <code>func textFieldDidEndEditing</code>, this wont work. I read a tip to assign tags to each input. Still, I would have to manually account for each one like so:</p>

<pre><code>func textFieldDidEndEditing(textField: UITextField) {
        if (textField.tag == 1) {
            txt1.text = textField.text
        }

        if (textField.tag == 2) {
            txt2.text = textField.text
        }

        if (textField.tag == 3) {
            txt3.text = textField.text
        }

        if (textField.tag == 4) {
            txt4.text = textField.text
        }

        if (textField.tag == 5) {
            txt5.text = textField.text
        }

        if (textField.tag == 6) {
            txt6.text = textField.text
        }
    }
</code></pre>

<p>Again, this seems pretty dumb that everyone does it like this..</p>

<p><strong>What I Need</strong></p>

<p>Either </p>

<p><strong>A)</strong> to be able to call the text field by a variable</p>

<pre><code>var dynText = textField
dynText.text = textField.text
</code></pre>

<p>However, this throws an error saying variable dynText doesn't have a member of text.</p>

<p><strong>B)</strong> to know which object (text box) hit/called the delegate in the first place.</p>

<p>How can I solve this? I doubt you guys are manually defining each and every text box in your apps!</p>