-9

I have set the placeholder text(e.g. Under 18s) of a text field(homeTeam) in storyboard. Now i want to check the condition something like below.

- (BOOL)textFieldShouldBeginEditing:(UITextField *) textField
{
    if ([_homeTeam.text isEqualToString:@"e.g. Under 18s"]) { // e.g. Under18s
        [_homeTeam setPlaceholder:@""];
    }
}

But the condition is never getting true. What am i missing here? _homeTeam.text is blank when i used NSlog to trace. Why is it so?

Please suggest.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Lalit_vicky
  • 295
  • 1
  • 5
  • 15
  • try this link http://stackoverflow.com/questions/1328638/placeholder-in-uitextview – codercat Nov 28 '13 at 06:59
  • 2
    What is the use of setting Place holder to @"" inside the `textFieldShouldBeginEditing` ? When you type a character in the text field it'll automatically hide the place holder. So what are you trying to do with this code ? – Midhun MP Nov 28 '13 at 07:03

2 Answers2

2

change your if condition, it should be _homeTeam.placeholder not _homeTeam.text because you set placeholder text not a text of UITextField.

- (BOOL)textFieldShouldBeginEditing:(UITextField *) textField
{
    if ([_homeTeam.placeholder isEqualToString:@"e.g. Under 18s"]) { // e.g. Under18s
        [_homeTeam setPlaceholder:@""];
    }
}
iPatel
  • 46,010
  • 16
  • 115
  • 137
1

You actually want to compare against the placeholder property, not text (which is input by the user).

Also, I would instead refer to textField (instead of _homeTeam directly, as you might add other text fields later), as such:

- (BOOL)textFieldShouldBeginEditing:(UITextField *) textField
{
    if ([textField.placeholder isEqualToString:@"e.g. Under 18s"]) { // e.g. Under18s
        [textField setPlaceholder:@""];
    }
}
JRG-Developer
  • 12,454
  • 8
  • 55
  • 81