4

I'm making an easy app where I have textFields. When the app starts I have examples in my textfields. I want these text examples to be deleted when users tap on textField. Can anybody help me?

Peter David Carter
  • 2,548
  • 8
  • 25
  • 44
lucca910
  • 91
  • 1
  • 9
  • 2
    Instead of setting the `text` property of these fields you should simply set the `placeholder` property. – Rok Jarc May 24 '16 at 17:09

3 Answers3

4

You can set Placeholder text in UITextField. Whenever, UITextField is tapped placeholder text disappears.

Alternatively, you can use following UITextFieldDelegate method

Objective-C

 - (void)textFieldDidBeginEditing:(UITextField *)textField {
   textField.text = ""
}

Swift

func textFieldDidBeginEditing(textField: UITextField) {
  textField.text = ""
}
Ashish Verma
  • 1,776
  • 1
  • 16
  • 27
  • 1
    Finally an answer that makes sense. – Rok Jarc May 24 '16 at 17:49
  • There is only one problem here: any text would be cleared in your case (when using the `text` instead of the `placeholder` property) as soon as user begins editing. One should at least compare the content to 'initial' content and clear if (and only if) they are a match. – Rok Jarc May 24 '16 at 18:02
  • 1
    It's not clear from question what exactly @lucca910 wants to acheive. Whether he wants to delete text every time like in some cases of Passwords. – Ashish Verma May 24 '16 at 18:16
1

Just add this underneath your didMoveToView method in youre SpriteKit Game

override func touchesBegan(touches: Set<UITouch>, with Event event: UIEvent?) {

for touch in touches {
    let location = touch.locationInNode(self)
       if nodeAtPoint(location) = textField {
          textField.removeFromParent()
            }
       }
     }
0

I think you are trying to create text field with placeholder.

this link might helps you.

Placeholder in UITextView

Community
  • 1
  • 1
  • 2
    Why are you linking to a question about putting a placeholder in a `UITextView`? This question is about `UITextField`. – rmaddy May 24 '16 at 17:18