I'm new to objective-c/ios and I'm a tad confused as to how to access a var/instance from another instance. I'm also using the Sparrow-frame work which might confuse things more but anyway.
The situation is that I have an instance of a class, called Game. Inside game I have a var called scrollContainer, which is itself a pointer to an instance of class type SPSprite.
I'm able to use scrollContainer no problem obviously inside Game, but now I need to access scrollContainer from another instance (called Market), and that's where I'm unsure.
I was declaring (my terminology is probably all wrong) scrollContainer inside Game.m, so I thought what I need to do is actually declare (define?) it in Game.h, with @property, and then @synthasize and then if I include Game.h in Market.m then I would be able to access the scrollContainer var from inside Market.m but that's not working, as it's giving me an error in Market.m (undeclared indentifier)
So my next thought is these are my options...
Pass the scrollContainer pointer to the Market.m init function when it's first called and store it?
I also have a singleton on the go, so maybe I store scrollContainer, or perhaps a pointer to Game.m in my singleton and they try to access scrollContainer from Market.m from that?
Which is the best/simplest way to go?
Thanks for any advice!
UPDATE
Here's the code snippet requested.
@interface Game : SPSprite
{
@private
float mGameWidth;
float mGameHeight;
}
- (id)initWithWidth:(float)width height:(float)height;
@property (nonatomic, assign) float gameWidth;
@property (nonatomic, assign) float gameHeight;
@property SPSprite *scrollContainer;
@end
And a bit from Market.m
- (void)onCloseMarketButton:(SPEvent *)event
{
NSLog(@"Close Market Clicked!!");
[self removeAllMarketButtons];
[closeMarketButton removeFromParent];
scrollContainer.visible = YES;//gives error
[self removeFromParent];
}
I just thought though, do I have to @synthesize scrollContainer in Market.m as well to be able to use it in Market.m?
In your code, you wrote:
scrollContainer.visible = YES;//gives error
How is scrollContainer
initialized or assigned?
You mentioned:
Pass the scrollContainer pointer to the Market.m init function when it's first called and store it?
Yes, this is a feasible approach.
//Some where in Game.m
Market *market = [[Market alloc]initWithScrollContainer:self.scrollContainer];
//Then in Market.m
-(id)initWithScrollContainer:(SPSprite*)scrollContainer{
//Do your initialization
}