UITextField Cocos2d中的示例 [英] UITextField Example in Cocos2d
问题描述
任何人都可以在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];
}
这将在键盘结束编辑时按return
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;
}
}
现在,某处。我从一个Scene类中调用它,以响应用户操作:
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];
这篇关于UITextField Cocos2d中的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!