1

I'm new to Swift. I managed to build an app which almost works, but I cannot get the last steps right. I would appreciate any help!

I wrote a code which displays a user-defined number of UILabels. In those labels, the contents of a [String] is displayed. Under the labels, there is the same number of UITextFields. The user should fill out these text fields, press the button and then see if what he filled out matches the labels.

All the labels, the text fields, and the button are made completely programmatically, so without using the storyboard. In the viewDidLoad there is all the code and this line:

myButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)

Right under viewDidLoad there is this function which I found on this forum and I changed it a bit:

@objc func buttonAction(sender: UIButton!) -> [String] {
    var a = 0
    var userInput: [String] = Array()
    while a < 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
}

My problems:

The while-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 myButton.addTarget line.

How do I read out the return in the viewdidload to add there the code to compare the user input with the original [String]?

Yury Imashev
  • 2,068
  • 1
  • 18
  • 32
Emile
  • 41
  • 1
  • 9
  • How exactly the user does define the number of labels? – Yury Imashev Sep 26 '18 at 23:06
  • you can choose to store the user-defined number in the class attribute as a Int and then access it in buttonAction. Technically, you cannot add anyother input variable into the buttonAction function other than the button itself. – Deepika Sep 26 '18 at 23:44
  • Thank you Deepika, this worked! I made a separate class with a code I found here (and don't understand fully) but it works like a charm. I can use this to access the Int in the button function and also to get the other variables from the viewdidload-section to check the answer. – Emile Sep 27 '18 at 08:27
  • After that, I'm stuck again, but I guess it's best to open a new question about that... – Emile Sep 27 '18 at 08:28

1 Answers1

0

You should consider the source of the user-defined input if you want to answer your question.

For instance, if you are willing to add an extra UITextField to retrieve your user input, then all you have to do is extract the value from that text field within your buttonAction(sender:) method and use it there. This translates roughly to the following

@objc func buttonAction(_ sender: UIButton) {

    var a = 0
    var userInput: [String] = Array()

    guard let upperLimit = self.userInputTextField.text as? Int else {
        return
    }

    while a < upperLimit {
        if let myTextField = self.view.viewWithTag(a) as? UITextField {
            let tekstInput = myTextField.text
            userInput.insert(tekstInput!, at: a-1)
        }
        a = a + 1
    }

}

Note that self.userInputTextField is the extra text field you should add in order to retrieve your user-defined input.

Jad Ghadry
  • 245
  • 2
  • 25
  • A button action can't have a return value. – rmaddy Sep 26 '18 at 23:12
  • Updated my answer @rmaddy. Well noticed mate. – Jad Ghadry Sep 27 '18 at 00:43
  • Thank you Jad for the answer. I should have been more clear about the user-defined input, it actually happened on another viewController, so I'm not sure if your code would work in this case. But for sure it gives me ideas for the future! – Emile Sep 27 '18 at 08:29
  • No worries @Emile! What you can do in that case is create a protocol to pass the parameter from one view controller to another. [This article](https://medium.com/@jamesrochabrun/implementing-delegates-in-swift-step-by-step-d3211cbac3ef) explains the concept I am talking about quite well. – Jad Ghadry Sep 27 '18 at 10:00