开发者

how to declare an array globally

hi i am new to iphone. what i a开发者_开发技巧m 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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜