Game Development Reference
In-Depth Information
If you try this now, you'll see a text field on your scene, and when you tap the text
field, the iPhone keyboard will come up, and you can start editing text. No extra code
needed. Except the keyboard won't go away anymore.
That's by design because the Return key might be a valid key to start a new line rather
than to stop editing. So, you need some way to dismiss the keyboard. To do so, open
the HelloWorldLayer header file and add the UITextFieldDelegate protocol
like so:
@interface HelloWorldLayer : CCLayer < UIAlertViewDelegate, UITextFieldDelegate>
Doing so allows the HelloWorldLayer class to respond to UITex-
tFieldDelegate methods like textFieldShouldReturn . For this to work,
you must assign the HelloWorldLayer class instance to the UITextField by as-
signing self to the delegate property. Add the bold line at the end of the initializa-
tion block of the UITextField :
// regular text field with rounded corners
UITextField* textField = [[UITextField alloc] initWithFrame:←
CGRectMake(40, 20, 200, 24)];
textField.text = @" Regular UITextField";
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.delegate = self;
Most UIKit views have this delegate method and an accompanying delegate protocol.
So, if you ever wonder how you can respond to events of a certain UIView , it's usu-
ally done by implementing the class's accompanying delegate protocol and responding
to the appropriate message. Of course, one very common and repeated mistake you'll
make (I know I do) is to forget to actually assign the delegate to the class interface. So,
whenever a delegate method isn't being called, or you get a compiler warning on the
line where you assign the delegate object, you should check whether that object's class
uses and implements the view's delegate protocol.
In our case, the textFieldShouldReturn message of the UITex-
tFieldDelegate protocol is sent whenever the user taps the Return key on the
iPhone keyboard:
Search Nedrilad ::

Custom Search