UITextField Example in Cocos2d(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 示例
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01