iosobjective-ccocos2d-iphonecocos2d-iphone-3

How can you programmatically bring up keyboard with CCTextField?


I have a CCTextField that is hidden until the user taps on an object, which then adds it to the node tree and it's visible.

Currently, the user must then tap the CCTextField to activate the iOS keyboard and begin entering text.

Is there a way to programmatically initiate the keyboard appearing ?

becomeFirstResponder isn't working.


Solution

  • I dug a little deeper into the CCTextField class code, and cocos2D adds the UITextField property as a subview of the cocos2d view.

    For some reason though, this didn't work :

    [myCCTextField.textField becomeFirstResponder];

    This is what I did, and it worked :

    [[CCDirector sharedDirector] view]subviews[0] becomeFirstResponder];

    If you have multiple CCTextFields you are obviously going to have to determine the index of your desired text field that you'd like to edit.