【发布时间】: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