title: How to programmatically receive input from UIButton in Swift 4?
body: <p>I'm new to <code>Swift</code>. I managed to build an app which almost works, but I cannot get the last steps right. I would appreciate any help!</p>
  
  <p>I wrote a code which displays a user-defined number of <code>UILabel</code>s. In those labels, the contents of a <code>[String]</code> is displayed. Under the labels, there is the same number of <code>UITextField</code>s. The user should fill out these text fields, press the button and then see if what he filled out matches the labels.</p>
  
  <p>All the labels, the text fields, and the button are made completely programmatically, so without using the storyboard. In the <code>viewDidLoad</code> there is all the code and this line:</p>
  
  <pre><code>myButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
  </code></pre>
  
  <p>Right under <code>viewDidLoad</code> there is this function which I found on this forum and I changed it a bit:</p>
  
  <pre><code>@objc func buttonAction(sender: UIButton!) -&gt; [String] {
      var a = 0
      var userInput: [String] = Array()
      while a &lt; 4 {
          if let myTextField = self.view.viewWithTag(a) as? UITextField {
              let tekstInput = myTextField.text
              userInput.insert(tekstInput!, at:a-1)
          }
          a = a + 1
      }
      return userInput
  }
  </code></pre>
  
  <p>My problems:</p>
  
  <p>The <code>while</code>-loop in the function shouldn't have 4 as the maximum, but a user-defined variable. But if I try to change function so that it expects that variable as an input, I get error messages in the <code>myButton.addTarget</code> line.</p>
  
  <p>How do I read out the return in the viewdidload to add there the code to compare the user input with the original <code>[String]</code>?</p>