Game Development Reference
In-Depth Information
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
// dismiss the keyboard
[textField resignFirstResponder];
// if the text is empty, remove the text field
if (textField.text.length == 0)
{
[textField removeFromSuperview];
}
return YES;
}
By sending the resignFirstResponder message to the textField , the key-
board will be dismissed. Simply as an exercise on how to remove a UIView from the
cocos2d view, I've added a condition that sends the removeFromSuperview mes-
sage to the textField if the textField is empty when the user presses Return.
Notice how this entire method doesn't care which UITextField is sending the mes-
sage, nor does it care where in the view hierarchy the textField was added. You'll
take advantage of that next by adding another UITextField .
If you try what you have so far, you'll notice that the keyboard is dismissed when you
press Return, and if you've deleted all characters from the text field, the entire text
field will vanish.
Tip Keep in mind that if it's possible that your scene changes while the user is
editing text in a UITextField , you would have to manually send the
resignFirstResponder message to all text fields in order to dismiss the
keyboard. Otherwise, the keyboard may remain visible during and after the
scene change, and the user won't be able to dismiss it anymore. To avoid this
situation, it's preferable to also respond to the textFieldDidBeginEdit-
ing message and use that to temporarily disable any buttons or events that
could change the current scene. Then reenable the buttons or events when you
receive the textFieldShouldReturn message.
Search Nedrilad ::




Custom Search