Cocos2d 中的 UITextField 示例

UITextField Example in Cocos2d(Cocos2d 中的 UITextField 示例)

本文介绍了Cocos2d 中的 UITextField 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能推荐一些在 cocos2d 中使用 UITextField 的链接.我想按下标签,然后应该选择 UITextField 并且我需要在该 UITextField 上进行编辑.

Can anyone please suggest some links for using UITextField in cocos2d. I want to press on label, then the UITextField should get selected and I need to edit on that UITextField.

推荐答案

我在当前项目中这样做是为了允许输入要开始播放的关卡数,所以这就是为什么我的变量和方法被命名为他们的样子;您可能应该调整这些以使其对您有意义.

I'm doing this in a current project to allow for entering the number of the level to start playing at, so that's why my variables and methods are named the way they are; you should probably adjust these to make sense for you.

在您的应用控制器中,将其定义为实例变量:

In your app controller, define this as an instance variable:

  UITextField *levelEntryTextField;

在 applicationDidFinishLaunching 中创建它:

Create it inside applicationDidFinishLaunching:

  levelEntryTextField = [[UITextField alloc] initWithFrame:
                                              CGRectMake(60, 165, 200, 90)];
  [levelEntryTextField setDelegate:self];

定义激活文本字段的方法.您还应该在应用控制器的头文件中声明它.

Define a method to activate the text field. You should also declare it in the header file for your app controller.

- (void)specifyStartLevel
{
    [levelEntryTextField setText:@""];
    [window addSubview:levelEntryTextField];
    [levelEntryTextField becomeFirstResponder];    
}

这将使在键盘上按返回"结束编辑

This will make pressing "return" on the keypad end editing

- (BOOL)textFieldShouldReturn:(UITextField*)textField {
  //Terminate editing
  [textField resignFirstResponder];
  return YES;
}

这是在实际完成编辑时触发的.

This is triggered when the editing is actually done.

- (void)textFieldDidEndEditing:(UITextField*)textField {
    if (textField==levelEntryTextField) {
        [levelEntryTextField endEditing:YES];
        [levelEntryTextField removeFromSuperview];
        // here is where you should do something with the data they entered
        NSString *result = levelEntryTextField.text;
    }
}

现在要真正启动事物,你可以把它放在某个地方.我从我的一个场景类中调用它,以响应用户操作:

Now to actually set things in motion, you put this somewhere. I call this from within one of my Scene classes, in response to a user action:

  [[[UIApplication sharedApplication] delegate] specifyStartLevel];

这篇关于Cocos2d 中的 UITextField 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Cocos2d 中的 UITextField 示例

基础教程推荐