关于textfield的使用

来源:互联网 发布:赛诺数据是什么 编辑:程序博客网 时间:2024/05/17 09:32

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
0 0
原创粉丝点击