hi i am new to iphone. what i am doing is declaring a NSMutable array named as labels, and that is declared in viewdidload.But when i access the label in buttonclick function it shows null. Previously i declred propery in .h file and initialize in init function also. please help where can i declare and how to declare a mutable array that is accesble any where in the class and how to add objects to it,that to the values are changeble dynamically. thanku
A:
you can declare them just as you would in C. when I need them, I have an include file variables.h which contains something like
ifdef GLOBAL
Int xyz; Struct abc *ptr;
else
External int xyz; External struct abc *ptr;
endif
in one .m file, do a #define GLOBAL
please excuse the typos, iPad is correcting me.
No One in Particular
2010-08-13 05:29:21
I don't think he's trying to create a global variable, but one that can be accessed from anywhere in the class. In any case, for global variables in the context of Objective-C, a singleton class would be more appropriate than declaring them as one would declare global C variables.
anshuchimala
2010-08-13 05:47:15
A:
If you want the array to be accessible anywhere in your class, you should declare it in your @interface
. Your code should look something like this:
@interface MyViewController : UIViewController {
NSMutableArray *labels;
}
@end
@implementation MyViewController
- (void)viewDidLoad {
labels = [[NSMutableArray alloc] init];
[labels addObject:@"Label"];
// etc.
}
- (void)dealloc {
// Don't forget to do this or your array will leak
[labels release];
[super dealloc];
}
- (void)buttonClickHandler {
// Do stuff with labels
}
@end
anshuchimala
2010-08-13 05:42:17