如何在SpriteKit上创建一个UITextField

7

我想在我的SpriteKit游戏中实现一个输入名字的文本框,所以我有以下代码:

SKLabelNode* gameTitle = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
gameTitle.text = @"Game Title";
gameTitle.name = @"gametitle";
gameTitle.fontSize = 44.0;
gameTitle.fontColor = [SKColor blackColor];
gameTitle.position = CGPointMake(self.size.width/2,self.size.height/2 + 150);
[self addChild:gameTitle];

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textColor = [UIColor blackColor];
textField.font = [UIFont systemFontOfSize:17.0];
textField.placeholder = @"Enter your name here";
textField.backgroundColor = [SKColor whiteColor];
textField.autocorrectionType = UITextAutocorrectionTypeYes;
textField.keyboardType = UIKeyboardTypeDefault;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self.delegate;

[self.view addSubview:textField];

所以,问题在于我的场景中没有显示它,也许问题出在添加到场景中时,我使用了addSubview方法。

这样做有问题吗?


尝试使用UITextView。 - CodeSmile
@LearnCocos2D 仍然没有出现 :/ - Vergmort
2个回答

17

您可以在 didMoveToView:(SKView *)view 函数中添加继承自 UIView 的对象。

只需将该函数添加到您的 SKScene 中并将您的 UITextField 移动即可:

-(void)didMoveToView:(SKView *)view {
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
    textField.center = self.view.center;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.textColor = [UIColor blackColor];
    textField.font = [UIFont systemFontOfSize:17.0];
    textField.placeholder = @"Enter your name here";
    textField.backgroundColor = [UIColor whiteColor];
    textField.autocorrectionType = UITextAutocorrectionTypeYes;
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.delegate = self.delegate;
    [self.view addSubview:textField];
}

1
非常棒且更有效的解决方案。谢谢! - Vergmort
1
谢谢你的答复。如果我切换到另一个场景,那么TextField会保留在视图中吗?我需要手动删除它吗? - palme
2
很遗憾,你将会 @palme。 - John Riselvato
感谢您的澄清。 - palme
谢谢!但是如何删除它? - Frade
[textField removeFromSuperView]; @Frade[textField removeFromSuperView]; @Frade - John Riselvato

1

要移除它,请使用

[myTextField removeFromSuperView];

如果您不想使用将textField插入场景展示的didMoveToView方法,您可以使用以下方法:
-(void)insertUI{

ttaAppDelegate *myDel = [[UIApplication sharedApplication] delegate];
UIViewController *vc = myDel.window.rootViewController;
self.textField= [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
self.textField.center = self.view.center;
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.textColor = [UIColor blackColor];
self.textField.font = [UIFont systemFontOfSize:17.0];
self.textField.placeholder = @"Enter your name here";
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.autocorrectionType = UITextAutocorrectionTypeYes;
self.textField.keyboardType = UIKeyboardTypeDefault;
self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
self.textField.delegate = self;
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.button addTarget:self action:@selector(saveScore:) forControlEvents:UIControlEventTouchDown];
[self.button setTitle:@"Save" forState:UIControlStateNormal];
self.button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);

[vc.view addSubview:self.textField];
[vc.view addSubview:self.button];
vc.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual;
[vc requestInterstitialAdPresentation];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接