Cocoa - 在另一个类中更新属性

时间:2011-03-28 18:28:27

标签: cocoa-touch class properties

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

我有两个应该一起工作的课程。在AnimalSelect类中,用户点击一个按钮。点击按钮后,用户将转到GamePlay类,并显示他选择的动物的名称。

我正在使用cocos2d来设置游戏。我知道我做了一些基本的错误,但我还没弄清楚它是什么。

到目前为止,这是我的简化代码:

AnimalSelect.h

@class GamePlay;

@interface AnimalSelect : CCLayer {
    GamePlay *gamePlay;
}

AnimalSelect.m

#import "GamePlay.h"
....

NSString *dierenNaam = @"Test";
gamePlay = [[GamePlay alloc] init];
gamePlay.dierenNaam = dierenNaam;

GamePlay.h

@interface GamePlay : CCLayer {
    NSString *dierenNaam;
}

@property (nonatomic, retain) IBOutlet NSString *dierenNaam;

GamePlay.m

@synthesize dierenNaam;
....
NSLog(@"Dierennaam is: %@", dierenNaam);

我知道我正在制作一个初学者错误,但我无法弄清楚我做错了什么。我希望有人可以帮助我。

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

修改 的 更新的代码:

titelLabel = [CCLabelTTF labelWithString:dierenNaam fontName:@“Marker Felt”fontSize:46]; titelLabel.position = ccp(160,430); [self addChild:titelLabel];

所以我调整了这样的代码:

GamePlay.h

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

- (void) updateLabel;

GamePlay.m

@synthesize titelLabel;
@synthesize dierenNaam;


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

AnimalSelect.m

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

===回到基础===

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

AnimalTestViewController.h

#import <UIKit/UIKit.h>
@class Animal;
@interface AnimalTestViewController : UIViewController {

    UILabel *titelLabel;
    UIButton *firstButton;

}

@property (nonatomic, retain) IBOutlet UILabel *titelLabel;
@property (nonatomic, retain) IBOutlet UIButton *firstButton;

- (IBAction) firstButtonPressed;

@end

AnimalTestViewController.m

#import "AnimalTestViewController.h"
#import "Animal.h"

@implementation AnimalTestViewController
@synthesize titelLabel;
@synthesize firstButton;
@synthesize animal;

- (void) firstButtonPressed {
    NSLog(@"The first button was pressed");
    NSString *newAnimal = @"Horse";
    NSLog(@"The variable newAnimal has a value of: %@", newAnimal);

    NSString *labelText = [[NSString alloc] initWithFormat:@"Initial text: %@", newAnimal];
    titelLabel.text = labelText;
    [labelText release];

    Animal *theAnimal = [[[Animal alloc] init];
    theAnimal.animalName = newAnimal;
    [theAnimal release];
}

Animal.h     #进口     @class AnimalTestViewController;     @interface Animal:NSObject {         NSString * animalName;     }

@property (nonatomic, retain) NSString *animalName;
@property (nonatomic, retain) NSString *title;

- (void) updateLabel;

@end

Animal.m

#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;
}

1 个答案:

答案 0 :(得分:0)

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

试试这个:

<强> AnimalSelect.h

@class GamePlay;

@interface AnimalSelect : CCLayer {
    GamePlay *gamePlay;
}

<强> AnimalSelect.m

#import "GamePlay.h"
....

NSString *dierenNaam = @"Test";
gamePlay = [[GamePlay alloc] init];
gamePlay.dierenNaam = dierenNaam;
[gamePlay logDierenNaam];

<强> GamePlay.h

@interface GamePlay : CCLayer {
    NSString *dierenNaam;
}

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

<强> GamePlay.m

@synthesize dierenNaam;

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

如果你不想拥有logDierenNaam函数,你可以为dierenNaam编写自己的setter和getter函数(并删除属性,并合成)并让setter记录值,但我不太熟悉这样做。我相信你可以在Google上找到一个指南。