【问题标题】:Cocoa - Update Property in an other classCocoa - 更新另一个类中的属性
【发布时间】:2011-03-28 18:28:26
【问题描述】:

我是 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 类中更新属性“dierenNaam”。

编辑 更新代码:

titelLabel = [CCLabelTTF labelWithString:dierenNaam fontName:@"Marker Felt" fontSize:46]; titleLabel.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]];
}

动物选择.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 动物:NSObject { NSString *动物名; }

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

【问题讨论】:

  • 我看到您正在尝试编辑您的帖子。如果您使用最初发布问题时使用的同一帐户登录,则不需要批准。请使用页面底部的“联系我们”链接请求网站管理员将您的帐户合并为一个。

标签: cocoa-touch class properties


【解决方案1】:

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 上找到指南。

【讨论】:

  • 但我想更新 GamePlay 类中的 dierenNaam 字符串并在 NSLog 中显示它的内容。我可以这样做吗?
  • 这确实是我的意思。感谢您帮助我。我想我可以处理你教给我的其余代码。
  • 哦,还有一个问题。在 GamePlay.m 的 init 函数中,我创建了一个 CCLabelTTF 来显示用户单击的动物的名称。是否可以在用户单击按钮后立即更新标签名称,以便在用户转到 GamePlay 类时显示正确的名称?
  • 我对 cocos2d 不是很熟悉,但是你不能在 GamePlay.h 中声明 CCLabelTTF,然后在类似于 logDierenNaam 的函数中设置标签的文本(你必须点击按钮时调用)?希望这会有所帮助。
  • 我更新了帖子中的代码(最后一点代码)。但这仍然返回一个空值。还是我也应该更改初始化函数?
猜你喜欢
  • 2020-03-05
  • 1970-01-01
  • 2011-07-02
  • 1970-01-01
  • 2020-02-21
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多