我是iPhone編程和可可中的新手,我似乎無法更新我在其他類中合成的屬性。可可 - 更新其他類中的屬性
我有兩個類應該一起工作。在AnimalSelect類中,用戶點擊一個按鈕。當點擊按鈕時,用戶將進入GamePlay類,並顯示他選擇的動物名稱。
我正在與cocos2d設置遊戲。我知道我正在做一些基本的事情,但我還沒有弄清楚它是什麼。
這是迄今爲止我simpliefied代碼:
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);
我知道我在做一個beginnersmistake,但我想不出什麼我做錯了。我希望有一個人可以幫助我。
@ edc1591 - 問題是我需要GamePlay類中的這些數據嗎?用戶在AnimalSelect類的視圖中選擇按鈕,然後轉移到GamePlay視圖。在這個視圖中,我想顯示所選動物的名稱。這就是爲什麼我需要在GamePlay類中更新「dierenNaam」屬性。
EDIT 更新代碼:
titelLabel = [CCLabelTTF labelWithString:dierenNaam的fontName:@ 「標記毛氈」 字體: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插座並將所有內容連接到筆尖文件中。這是我使用的代碼。所有NSLog方法都會執行,但標籤不會更新theUpdatedLabel variabel的文本。
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 {N/A} {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;
}
我看到你正在嘗試編輯你的文章。如果您使用過去用於發佈問題的相同帳戶登錄,則不需要審批。請使用頁面底部的「與我們聯繫」鏈接,要求網站管理員將您的帳戶合併到一個帳戶中。 – 2011-03-28 19:29:12