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 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01