tags:

views:

109

answers:

1

Hi All I have an array arryImages which I would only like to declare once and access it from a number of view controllers, but I can't figure out where to place it.

I have a separate class (Variables.m) which contains the following code

 @implementation Variables

//images array
arryImages = [NSArray arrayWithObjects:
              [UIImage imageNamed: @"ico-company.png"],
              [UIImage imageNamed: @"ico-value.png"],
              [UIImage imageNamed: @"ico-date.png"],
              [UIImage imageNamed: @"ico-notes.png"], nil];

@end

In my viewcontroller class (which I want to access the array) I have added:

#import "Variables.h"

I tried playing with @synthesize, but I can't access the array arrayImages.

(Using the call imageView2.image = [arryImages objectAtIndex:[indexPath row]];) Ps. this works perfect when its just above the code, but not in a separate class.

What am I missing?

Regards

+1  A: 

You should declare your array as a property. Synthesize it and initialize in your ViewDidLoad Method.

i.e. the header

@interface AddFriendViewController : UIViewController {
 NSArray *arryImages;
}

@property (nonatomic, retain) NSarray *arryImages ;

@end

the implementation:

@synthesize arryImages;

- (void)viewDidLoad {
    [super viewDidLoad];
    arryImages = [NSArray arrayWithObjects:
          [UIImage imageNamed: @"ico-company.png"],
          [UIImage imageNamed: @"ico-value.png"],
          [UIImage imageNamed: @"ico-date.png"],
          [UIImage imageNamed: @"ico-notes.png"], nil];
}

If you want to share a variable between views, you should put in your app delegate and access it from there.

NSArray *arryImages = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] arryImages];
Henrik P. Hessel
I am still trying to figure out exactly what you mean. And I want this to work from numerous viewcontrollers.Are you saying the first header should be in the appdelegate and the second bit in ViewDidLoad? As in i should create it? I only have applicationDidFinishStartingAnd then does the third code go in the view controller I want to access arryImages?For example, lets say the view controllers I want to access arryImages are :DetailViewController.mand EditViewController.m and I only want to declare the array once.what code goes where?Regards
norskben
You declare your NSArray in your AppDelegate Header, init and fill it in your AppDelegate Implementation and access it by using the code I posted above: "NSArray *arryImages = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] arryImages];"
Henrik P. Hessel
and yes: it's not viewdidload but applicationDidFinishStarting in your app delegate.
Henrik P. Hessel
ok its working ;) Your second comment help clear things up a bit. The appDelegate @property should be '@property (nonatomic, retain) NSArray *arryImages;' with Array capitalised. I declared the array in applicationDidFinishLaunching, as "self.arryImages = [NSArray arrayWithObjects: ..." and the third bit i put in just before where I needed the NSArray.Thanks for the help!
norskben
no problem and welcome to stackoverflow.com
Henrik P. Hessel