I was just curious about the correct way to go about accessing a property that I have declared in a ViewController and using it in an Objective-C Class file? I have included the .h file, but that doesn’t seem to be enough. I know that I could just move it to the app delegate and access it in both places then, but there has to be a cleaner and more direct way.
Edit for clarity:
nrViewController.h:
@interface nrViewController : UIViewController {
NSMutableArray *checkpoints;
}
@property (nonatomic, retain) NSMutableArray *checkpoints;
nrViewController.m
@implementation nrViewController
@synthesize checkpoints;
//A bunch of code, some of which actually uses checkpoints
@end
XMLParser.m:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Checkpoints"]) {
checkpoints = [[NSMutableArray alloc] init];
}
This isn’t totally complete as I would like to us it in more places, but the only other place outside of XMLParser.m would be in nrViewController.m and that I understand how to do that. I would like to declare and use the checkpoints variable in nrViewController, and use it in XMLParser.m as well as I would be loading parse data into it and then manipulating it. I hope this clarifies things a bit more, but if more info is need I would be happy to explain in even more detail.
Thanks!
-Karoly
To be honest, hard to follow the idea of question.
If you declared a property and synthesized or coded getters/setters, then it should be available from the other classes as long as you’re trying to get it from object of the class, where you declared the property.
If you want to use property as a static variable, touching just a class, not an object, then you can do that, but you should either define this variable as static global variable and work with it directly, or you should implement your custom static getter/setter methods for that property.
Hope I didn’t get confused in some place.
EDIT
Observations according to the provided code:
You didn’t declare a property, man. For your variable you should use
(this is under the closing figure bracket in @interface. And in .m file you should do
@synthesize checkpoints;(after the @implementation nrViewController { )
Also, access your variable in the way