Cocoa - Update Property in an other class
I am new to iPhone programming and in Cocoa I can't seem to update a property that I synthesized in an other class.
I have two classes which should work together. In the AnimalSelect class the users taps a button. When the button is tapped the user will go to the GamePlay class and the name of the animal he selected should be displayed.
I am working with cocos2d for setting up the game. I know I am doing some basic thing wrong but I haven't figured out what it is.
Here is my simpliefied code so far:
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);
I know I am making a beginnersmistake, but I can't figure out what I am doing wrong. I hope someone can help me.
@edc1591 - The problem is that I need this data in the GamePlay class? The user selects the button in the view of the AnimalSelect class and then is transfered to the GamePlay view. In this view I want to display the name of the selected animal. That's why I need the property "dierenNaam" to be updated in the GamePlay Class.
EDIT Updated code:
titelLabel = [CCLabelTTF labelWithString:dierenNaam fontName:@"Marker Felt" fontSize:46]; titelLabel.position = ccp (160, 430); [self addChild:titelLabel];
So I adjusted the code like this:
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];
=== back to basics ===
Here is the new code I tried, because I wanted to go back to basics and get the label updated with just xCode. I created the UILabel outlets and connected everything in the nib-file. Here is the code I used. All NSLog methods are executed, but the label is not updated with the text of the theUpdatedLabel variabel.
AnimalTestViewController.h
#import <UIKit/UIKit.h>
@class Animal;
@interface AnimalTestViewController : UIViewController {
UILabel *titelLabel;
开发者_如何学JAVAUIButton *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 #import @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;
}
The NSLog has to be inside a function that is called from the AnimalSelect class. It won't log anything otherwise.
Try this:
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);
}
If you don't want to have that logDierenNaam function, you could write your own setter and getter function for dierenNaam (and remove the property, and synthesize) and have the setter log the value, but I'm not too familiar with doing that. I'm sure you could find a guide on google.
精彩评论