关于textfield的使用

本文介绍如何在Cocos2d-x 0.7.1版本中集成UITextField,实现触摸屏幕显示输入框并获取用户输入的功能。通过在AppDelegate.m文件中修改,创建了一个仅在需要时出现的UITextField,节省了内存资源。

I took the example that Jack provided and actually created a working project, this was done using the Cocos2D 0.7.1 XCode Template, and then just editting the *AppDelegate.m/.h files, which are provided below in there entirety. I also modified some of what Jack said, because I feel that creating the UITextField in the appDidFinishLoading would utilize a bit too much memory, especially if the text field is not used all the time ... this solution creates the text field only when it is needed, the sample draws an empty Cocos2D Layer scene, and on screen touch, it displays the text field for you to start entering text into. It will spit out the result of what you entered to the Console - you can pass this to whatever is necessary in your own code.

the .h

#import <UIKit/UIKit.h>
#import "cocos2d.h"
@interface MYSCENE :Layer<UITextFieldDelegate>
{
   
UITextField*myText;
}
-(void)specificStartLevel;
@end
@interface textFieldTestAppDelegate :NSObject<UIAccelerometerDelegate,UIAlertViewDelegate,UITextFieldDelegate,UIApplicationDelegate>
{
   
UIWindow*window;
}
@end

and then the .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
link | edit

转载于:https://www.cnblogs.com/wangcan/articles/2461375.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值