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 ad开发者_如何学Goded:
#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
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];
精彩评论