将文本域中的文本从一个场景更新到另一个场景

时间:2009-04-09 00:43:10

标签: cocos2d-iphone textfield

我在将数据(textField文本)从一个场景放到另一个场景时遇到了问题。    我正在用这封邮件附上我的源代码。   如果我的代码有任何问题,请帮助我。

//
//  PlayerSettingsAppDelegate.m
//  PlayerSettings
//
// 
//

#import "PlayerSettingsAppDelegate.h"
#import "TestScene.h"

NSString *result;

@implementation MYSCENE
-(id) init
{
      [super init];

        id layer = [ColorLayer layerWithColor: 0x2266FFff];
        [self add:layer z:0];
        [MenuItemFont setFontSize:15];
        [MenuItemFont setFontName:@"Helvetica"];
        MenuItem *backMenu = [MenuItemFont itemFromString:@"Back" target:self selector:@selector(menuBack:)]
;
        Menu *menu = [Menu menuWithItems:backMenu,nil];

        [menu alignItemsVertically];
        [self add:menu];

    /*Text field creation*/
    myTextField = [[UITextField alloc]initWithFrame:CGRectMake(60, 165, 100, 10)];
    myTextField.placeholder=@"<Enter text>";
    [myTextField setDelegate:self];
    [window addSubview:myTextField];


    return self;
}

-(void)menuBack:(id)sender{
    NSLog(@"Back");
    TestScene *ts = [TestScene node];
    [[Director sharedDirector]replaceScene:ts];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if(textField==myTextField){
        [myTextField endEditing:YES];

        result = myTextField.text;
        [label setString:result];
        [myTextField removeFromSuperview];
    }
}

-(void) dealloc
{
    [super dealloc];
}
@end

@implementation MenuLayer

-(id)init {
    self = [super init];



    return self;
}




@end


@implementation PlayerSettingsAppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    // NEW: Init the window
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [window setUserInteractionEnabled:YES];
    //[window setMultipleTouchEnabled:YES];

    //[[Director sharedDirector] setLandscape: YES];
    [[Director sharedDirector] setDisplayFPS:YES];

    [[Director sharedDirector] attachInWindow:window];

    Scene *scene = [Scene node];
    [scene add: [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

-------------------------------------------------------------------------
//
//  TestScene.m
//  PlayerSettings
//
// 
//

#import "TestScene.h"
#import "cocos2d.h"
#import "PlayerSettingsAppDelegate.h"

UITextField *levelEntryTextField;

@implementation TestScene
-(id)init{
    [super init];
    Sprite *bg = [Sprite spriteWithFile:@"image1.jpg"];
    [bg setPosition:cpv(160,240)];
    [self add:bg z:0];

    label = [Label labelWithString:@"result" fontName:@"Helvetica" fontSize:20.0];
    label.position = cpv(100,100);
    [self add:label z:1];

    return self;
}



@end

1 个答案:

答案 0 :(得分:0)

不完全确定,但我相信

label = [Label labelWithString:@"result" fontName:@"Helvetica" fontSize:20.0];

应该阅读

label = [Label labelWithString:result fontName:@"Helvetica" fontSize:20.0];