1

I created multiple text fields programatically. Their input is being read and after the answer is checked a new question is being posed to the user. But I don't seem to be able to clear the text fields, the previous input is still there. I tried already to add textfield.text = "" but it doesn't seem to work. Maybe I should somehow delete the textfield with the correct tag, but I don't know how to do that. Does anybody have a suggestion for this situation?

var a = 0
while a < aantalNoten {
    let myTekstveld = UITextField()
    if (view.viewWithTag(a+1) as? UITextField) != nil {
        myTekstveld.text = ""
        }
        else {
            myTekstveld.frame = CGRect(x: labelX, y: labelY + 100, width: labelWidth, height: labelHeight / 2)
            myTekstveld.backgroundColor = UIColor.white
            myTekstveld.textAlignment = .center
            myTekstveld.placeholder = "?"
            myTekstveld.keyboardType = UIKeyboardType.default
            myTekstveld.borderStyle = UITextField.BorderStyle.line
            myTekstveld.autocorrectionType = .no
            myTekstveld.returnKeyType = UIReturnKeyType.done
            myTekstveld.clearButtonMode = UITextField.ViewMode.whileEditing
            myTekstveld.textColor = UIColor.init(displayP3Red: CGFloat(96.0/255.0), green: CGFloat(35.0/255.0), blue: CGFloat(123.0/255.0), alpha: 1)
            myTekstveld.delegate = self as? UITextFieldDelegate
            myTekstveld.tag = a + 1
            view.addSubview(myTekstveld)
        }
        a += 1
        labelX += labelWidth
    }
Emile
  • 41
  • 1
  • 9
  • You should maintain the text fields you create in an array and just iterate over them and clear them when needed. You seem to clearing the text of the new text field you create just before checking, which hasn't even been added to the view yet. Clear the existing text field. – Rakesha Shastri Sep 30 '18 at 14:35

3 Answers3

3

You have to find the textfield with tag. Here view is the super most view. Loop through all the subviews and find your textField.

for view in self.view.subviews {
   //Get One By One with Tag
   if let txtField1 = self.view.viewWithTag(tagOfTextField) as? UITextField {
       txtField1.text = ""
   }
}

Or you can clear all the textfields

for view in self.view.subviews {
    //Get All Values
    if let textField = view as? UITextField {
        textField.text = ""
    }
}
Arnab
  • 4,216
  • 2
  • 28
  • 50
1

The problem is this line:

let myTekstveld = UITextField()

That creates a new local variable myTekstveld that only exists in the current scope. (Usually the innermost set of curly braces that enclose the code.) You are creating a text field, adding it to your view hierarchy, and then promptly forgetting about it.

You probably have an instance variable myTekstveld as well, but you are not setting that. That instance variable likely contains a DIFFERENT text field.

Remove the word let from your code and it will save your newly created text field to your instance variable

Add a new line just above your myTekstveld = UITextField() line that says:

print("myTekstveld = \(myTekstveld)") 

And see what it displays to the debug console.

If it logs a text field, you should probably get rid of the myTekstveld = UITextField() line entirely and just configure the existing text field rather than replacing the text field with a new one.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
-4

Check this :

  textfield.text = nil
Am1rFT
  • 227
  • 5
  • 18
  • 2
    Please provide details on how this code works, compared to the question? When you explain your answer in addition to the code, it will be helpful to all the readers. – Prabhu Oct 01 '18 at 07:42
  • it will work in an action, like when a button is pressed. – Am1rFT Oct 01 '18 at 07:44
  • Rather than just guess at answers, it's worth trying them out in a playground first – Ashley Mills Oct 02 '18 at 09:09