1

我需要创建一个文本字段,我的游戏中的玩家可以在其中输入他们的名字以获取高分。我跟着这个问题:UITextField Example in Cocos2d,并成功地用键盘创建了一个文本字段。但是,文本字段的文本是白色的,背景颜色也是白色的,所以我看不到文本字段,只有键盘。

这是代码:

应用代理.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);
}
}

正在从另一个类/按钮调用该方法

    [[[UIApplication sharedApplication] delegate]performSelector:@selector(specifyPlayerName)];

我试图设置文本字段的颜色,但它仍然显示为全白。任何帮助,将不胜感激。

4

1 回答 1

1

我发现文本字段隐藏在另一个视图后面,所以

  [[[CCDirector sharedDirector] openGLView] addSubview:nameTextField];

代替

[[[[CCDirector sharedDirector] openGLView] window] addSubview:nameTextField];

文本字段颜色默认为白底黑字。

于 2013-06-27T06:57:18.897 回答