I need to create a textfield where a player in my game can enter their name for highscore purposes. I followed this question: UITextField Example in Cocos2d , and successfuly created a textfield with the keyboard. However, the textfield's text is white and the background color is also white so I cannot see the textfield, only the keyboard.
Here is the code:
appdelegate.h
#import "KKAppDelegate.h"
@interface AppDelegate : KKAppDelegate
{
UITextField *nameTextField;
}
-(void)specifyPlayerName;
@end
appdelegate.m
@implementation AppDelegate
-(void) initializationComplete
{
#ifdef KK_ARC_ENABLED
CCLOG(@"ARC is enabled");
#else
CCLOG(@"ARC is either not available or not enabled");
#endif
}
- (void)specifyPlayerName
{
NSLog(@"called");
nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(60, 165, 200, 90)];
[nameTextField setDelegate:self];
[nameTextField setText:@""];
[nameTextField setTextColor: [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0]];
[[[[CCDirector sharedDirector] openGLView] window] addSubview:nameTextField];
[nameTextField becomeFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField*)textField {
//Terminate editing
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField*)textField {
if (textField==nameTextField) {
[nameTextField endEditing:YES];
// here is where you should do something with the data they entered
GameData* data = [GameData sharedData];
data.playerName = nameTextField.text;
NSLog(@"entered: %@", data.playerName);
}
}
The method is being called from another class/button
[[[UIApplication sharedApplication] delegate]performSelector:@selector(specifyPlayerName)];
I tried to set the color of the textfield, but it is still showing up as all white. Any help would be appreciated.
I found out that the textfield was hidden behind the other view, so
[[[CCDirector sharedDirector] openGLView] addSubview:nameTextField];
instead of
[[[[CCDirector sharedDirector] openGLView] window] addSubview:nameTextField];
The textfield colors are default to be black on white.