Cocoa touch Cocoa-其他类中的更新属性

Cocoa touch Cocoa-其他类中的更新属性,cocoa-touch,class,properties,Cocoa Touch,Class,Properties,我是iPhone编程新手,在Cocoa中,我似乎无法更新在其他类中合成的属性 我有两门课,应该一起上。在AnimalSelect类中,用户点击一个按钮。点击按钮后,用户将进入游戏类,并显示所选动物的名称 我正在与cocos2d合作设置游戏。我知道我做错了一些基本的事情,但我还没有弄清楚是什么 以下是我迄今为止简化的代码: 动物选择 动物选择 gamePlay = [[GamePlay alloc] init]; gamePlay.dierenNaam = dierenNaam; [gamePla

我是iPhone编程新手,在Cocoa中,我似乎无法更新在其他类中合成的属性

我有两门课,应该一起上。在AnimalSelect类中,用户点击一个按钮。点击按钮后,用户将进入游戏类,并显示所选动物的名称

我正在与cocos2d合作设置游戏。我知道我做错了一些基本的事情,但我还没有弄清楚是什么

以下是我迄今为止简化的代码:

动物选择

动物选择

gamePlay = [[GamePlay alloc] init];
gamePlay.dierenNaam = dierenNaam;
[gamePlay updateLabel];
[gamePlay release];
游戏性

@property (nonatomic, retain) CCLabelTTF *titelLabel;
@property (nonatomic, retain) NSString *dierenNaam;

- (void) updateLabel;
游戏性

@synthesize titelLabel;
@synthesize dierenNaam;


- (void) updateLabel {
    [titelLabel setString:[NSString stringWithString:dierenNaam]];
}
我知道我在冒险,但我不知道我做错了什么。我希望有人能帮助我

@edc1591-问题是我在GamePlay类中需要这些数据?用户在AnimalSelect类的视图中选择按钮,然后切换到GamePlay视图。在此视图中,我想显示所选动物的名称。这就是为什么我需要在GamePlay类中更新属性dierenNaam

编辑 更新代码:

滴度标签=[CCLabelTTF labelWithString:dierenNaam fontName:@Marker Felt fontSize:46]; titelLabel.position=ccp 160430; [self-addChild:titelLabel]

所以我调整代码如下:

游戏性

@property (nonatomic, retain) CCLabelTTF *titelLabel;
@property (nonatomic, retain) NSString *dierenNaam;

- (void) updateLabel;
游戏性

@synthesize titelLabel;
@synthesize dierenNaam;


- (void) updateLabel {
    [titelLabel setString:[NSString stringWithString:dierenNaam]];
}
动物选择

gamePlay = [[GamePlay alloc] init];
gamePlay.dierenNaam = dierenNaam;
[gamePlay updateLabel];
[gamePlay release];
==回到基本面===

这是我尝试过的新代码,因为我想回到基础,用xCode更新标签。我创建了UILabel outlets并连接了nib文件中的所有内容。这是我使用的代码。所有NSLog方法都会执行,但标签不会用UpdatedLabel变量的文本更新

AnimalTestViewController.h

动物 进口 @类animaltestview控制器; @接口动物:NSObject{ NSString*动物名称; }

动物


NSLog必须位于从AnimalSelect类调用的函数中。它不会记录任何其他内容

试试这个:

动物选择

动物选择

gamePlay = [[GamePlay alloc] init];
gamePlay.dierenNaam = dierenNaam;
[gamePlay updateLabel];
[gamePlay release];
游戏性

@property (nonatomic, retain) CCLabelTTF *titelLabel;
@property (nonatomic, retain) NSString *dierenNaam;

- (void) updateLabel;
游戏性

@synthesize titelLabel;
@synthesize dierenNaam;


- (void) updateLabel {
    [titelLabel setString:[NSString stringWithString:dierenNaam]];
}

如果不想使用logDierenNaam函数,可以为dierenNaam编写自己的setter和getter函数,并删除属性,然后合成并让setter记录值,但我不太熟悉这样做。我相信你可以在谷歌上找到一本指南。

我看到你正在试图编辑你的帖子。如果您使用的帐户与您最初发布问题时使用的帐户相同,那么您就不需要批准。请使用页面底部的“联系我们”链接,请求站点管理员将您的帐户合并为一个帐户。但我想更新GamePlay类中的dierenNaam字符串,并在NSLog中显示其内容。我能那样做吗?那确实是我的意思。谢谢你帮了我。我想我可以处理你教我的其他代码。哦,还有一个问题。在GamePlay.m的init函数中,我创建了一个CCLabelTTF来显示用户单击的动物的名称。是否有可能在用户单击按钮后立即更新标签的名称,以便在用户进入GamePlay类时显示正确的名称?我对cocos2d不是很熟悉,但是你不能在GamePlay.h中声明CCLabelTTF,然后在一个类似于logDierenNaam的函数中设置标签的文本,当点击按钮时你必须调用这个函数吗?希望这有帮助。我在我的文章最后一段代码中更新了代码。但这仍然返回空值。或者我也应该更改init函数吗?
@property (nonatomic, retain) NSString *animalName;
@property (nonatomic, retain) NSString *title;

- (void) updateLabel;

@end
#import "Animal.h"
#import "AnimalTestViewController.h"

@implementation Animal

@synthesize animalName;
@synthesize aViewController;

- (void) updateLabel {

    NSLog (@"The variable animalName has a value %@", animalName);
    NSString *theUpdatedLabel = @"The label is updated";
    AnimalTestViewController *aViewController = [[AnimalTestViewController alloc] init];
    aviewController.titelLabel.text = theUpdatedLabel;
}
@class GamePlay;

@interface AnimalSelect : CCLayer {
    GamePlay *gamePlay;
}
#import "GamePlay.h"
....

NSString *dierenNaam = @"Test";
gamePlay = [[GamePlay alloc] init];
gamePlay.dierenNaam = dierenNaam;
[gamePlay logDierenNaam];
@interface GamePlay : CCLayer {
    NSString *dierenNaam;
}

@property (nonatomic, retain) NSString *dierenNaam;
-(void)logDierenNaam;
@synthesize dierenNaam;

-(void)logDierenNaam {
     NSLog(@"Dierennaam is: %@", dierenNaam);
}