任何人都可以建议一些在cocos2d中使用UITextField的链接.我想按标签,然后UITextField
应该选择,我需要编辑UITextField
.
我在当前项目中执行此操作以允许输入开始播放的级别数,这就是为什么我的变量和方法按照它们的方式命名的原因; 你可能应该调整这些对你有意义.
在您的app控制器中,将其定义为实例变量:
UITextField *levelEntryTextField;
在applicationDidFinishLaunching中创建它:
levelEntryTextField = [[UITextField alloc] initWithFrame: CGRectMake(60, 165, 200, 90)]; [levelEntryTextField setDelegate:self];
定义激活文本字段的方法.您还应该在app控制器的头文件中声明它.
- (void)specifyStartLevel { [levelEntryTextField setText:@""]; [window addSubview:levelEntryTextField]; [levelEntryTextField becomeFirstResponder]; }
这将使键盘末端编辑按下"返回"
- (BOOL)textFieldShouldReturn:(UITextField*)textField { //Terminate editing [textField resignFirstResponder]; return YES; }
实际完成编辑时会触发此操作.
- (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类中调用它,以响应用户操作:
[[[UIApplication sharedApplication] delegate] specifyStartLevel];
我举了Jack提供的实例并实际创建了一个工作项目,这是使用Cocos2D 0.7.1 XCode模板完成的,然后编辑*AppDelegate.m/.h文件,这些文件在下面全部提供.我还修改了Jack说的一些内容,因为我觉得在appDidFinishLoading中创建UITextField会占用太多内存,特别是如果文本字段不是一直使用的话...这个解决方案只在它创建文本字段时需要时,示例将绘制一个空的Cocos2D图层场景,并在屏幕触摸时显示文本字段,以便您开始输入文本.它将吐出您输入控制台的结果 - 您可以将其传递给您自己的代码中所需的任何内容.
.h
#import#import "cocos2d.h" @interface MYSCENE : Layer { UITextField *myText; } -(void)specificStartLevel; @end @interface textFieldTestAppDelegate : NSObject { UIWindow *window; } @end
然后是.m
#import "textFieldTestAppDelegate.h" @implementation MYSCENE -(id) init { self = [super init]; isTouchEnabled = YES; return self; } -(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self specifyStartLevel]; return kEventHandled; } -(void)specifyStartLevel { myText = [[UITextField alloc] initWithFrame:CGRectMake(60, 165, 200, 90)]; [myText setDelegate:self]; [myText setText:@""]; [myText setTextColor: [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0]]; [[[[Director sharedDirector] openGLView] window] addSubview:myText]; [myText becomeFirstResponder]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField { [myText resignFirstResponder]; return YES; } -(void)textFieldDidEndEditing: (UITextField *)textField { if(textField == myText) { [myText endEditing:YES]; [myText removeFromSuperview]; NSString *result = myText.text; NSLog([NSString stringWithFormat:@"entered: %@", result]); } else { NSLog(@"textField did not match myText"); } } -(void) dealloc { [super dealloc]; } @end @implementation textFieldTestAppDelegate - (void)applicationDidFinishLaunching:(UIApplication *)application { window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [window setUserInteractionEnabled:YES]; [[Director sharedDirector] setDisplayFPS:YES]; [[Director sharedDirector] attachInWindow:window]; Scene *scene = [Scene node]; [scene addChild: [MYSCENE node]]; [window makeKeyAndVisible]; [[Director sharedDirector] runWithScene: scene]; } -(void)dealloc { [super dealloc]; } -(void) applicationWillResignActive:(UIApplication *)application { [[Director sharedDirector] pause]; } -(void) applicationDidBecomeActive:(UIApplication *)application { [[Director sharedDirector] resume]; } - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { [[TextureMgr sharedTextureMgr] removeAllTextures]; } @end