Koldo,You could create a singleton class and dump all of your data there, or you could set the parameter as a property on the view before you load it. I use the singleton method, but the easiest way is probably setting the property on the view before loading it. You could also define a message to pass the variable in through.I have pasted some code below from both the singleton and the property-set methods, use as you see fit.Here is the header file from my model singleton. Note that the sharedModel method declaration is denoted with a plus sign.#import <Foundation/Foundation.h>
#import "iLenexaMapView.h"
#import "ArcGIS.h"
@interface iLenexaModel : NSObject {
iLenexaMapView *lenMapView;
AGSTiledMapServiceLayer *basemap;
AGSTiledMapServiceLayer *crimemap;
AGSTiledMapServiceLayer *parksmap;
AGSTiledMapServiceLayer *aerials;
AGSGraphicsLayer *serviceRequests;
AGSPoint *currLocation;
NSString *lyrName;
NSString *lyrURL;
NSString *lyrType;
NSString *basemapName;
}
@property (nonatomic,retain) iLenexaMapView *lenMapView;
@property (nonatomic, retain) AGSTiledMapServiceLayer *basemap;
@property (nonatomic, retain) AGSTiledMapServiceLayer *crimemap;
@property (nonatomic, retain) AGSTiledMapServiceLayer *parksmap;
@property (nonatomic, retain) AGSTiledMapServiceLayer *aerials;
@property (nonatomic, retain) AGSPoint *currLocation;
@property (nonatomic, retain) AGSGraphicsLayer *serviceRequests;
@property (nonatomic, retain) NSString *basemapName;
@property (nonatomic, retain) NSString *lyrName;
@property (nonatomic, retain) NSString *lyrURL;
@property (nonatomic, retain) NSString *lyrType;
//note the plus sign in from of this method. We are going to need to call this from wherever we //want to collect the property from
+ (id)sharedModel;
@end
Here is the implementation file:#import "iLenexaModel.h"
static iLenexaModel *sharedMyModel = nil;
@implementation iLenexaModel
@synthesize lenMapView, crimemap, basemap,parksmap,serviceRequests, aerials;
@synthesize lyrType, lyrName, lyrURL, currLocation;
@synthesize basemapName;
+(id)sharedModel{
@synchronized(self){
if (sharedMyModel==nil) {
[[self alloc] init];
}
}
return sharedMyModel;
}
+ (id)allocWithZone:(NSZone *)zone{
@synchronized(self){
if (sharedMyModel == nil) {
sharedMyModel = [super allocWithZone:zone];
return sharedMyModel;
}
}
return nil;
}
-(id)copyWithZone:(NSZone *)zone{
return self;
}
-(id)retain{
return self;
}
-(unsigned)retainCount{
return UINT_MAX;
}
-(void)release{}
-(id)autorelease{
return self;
}
-(id)init{
if (self=[super init]) {
NSLog(@"Jeremy's rad singleton");
}
}
-(void)dealloc {
[lenMapView release];
[super dealloc];
}
@end
Setting a property in the model singleton (this one from a UIView): iLenexaModel *myModel = [iLenexaModel sharedModel];
myModel.lenMapView = self;
Another example of using/setting properties in a singleton: iLenexaModel *myModel = [iLenexaModel sharedModel];
mapViewer.myBasemap = myBasemap;
[mapViewer changeBasemap];
[self.navigationController pushViewController:mapViewer animated:YES];
if ([lyrType isEqualToString:@"Tiled"]) {
[mapViewer addLenexaTiledMapLayer:lyrURL :myModel.lyrName];
}
else if([lyrType isEqualToString:@"Dynamic"]){
[mapViewer addLenexaDynamicMapLayer:lyrURL :myModel.lyrName];
}
else if([lyrType isEqualToString:@"Graphic"]){
[mapViewer addLenexaGraphicsMapLayer:lyrURL :myModel.lyrName];
}
else{
NSLog(@"Something is wrong here!!!!");
}
mapViewer.lyrName = myModel.lyrName;
//and so on...
}
Setting properties on the view as a method of passing parameters between them is just a variant of this method, where you identify your properties in the view's header, set them, and then add the view to the user interface. In this brief example, I set the properties on the subTable view and then push it to the user: if([lyrName isEqualToString:@"Economic Development"]){
NSLog(@"Economic Development");
iLenexaTableSubView *subTable = [[iLenexaTableSubView alloc] initWithNibName:@"iLenexaTableSubView" bundle:[NSBundle mainBundle]];
NSString *path3 = [[NSBundle mainBundle] pathForResource:@"econDevelopment" ofType:@"plist"];
NSMutableArray* myarr2 = [[NSMutableArray alloc] initWithContentsOfFile:path3];
subTable.myVarArray = myarr2;
subTable.mapViewer = mapViewer;
subTable.myBasemap = myModel.basemap;
[myarr2 release];
[self.navigationController pushViewController:subTable animated:YES];
[subTable release];
}
Finally, you can also specify methods (I think in the iOS world they're called "messages") through which you can identify methods to pass and set variables after instantiating the view but before trying to use the properties you are trying to set:So you would first identify the method in your header file, then implement it in the implementation file. This is from the header file in my appDelegate:- (void) collectCrimeData;
And this is from the implementation file:-(void) collectCrimeData{
NSLog(@"We collect the crime data!!!");
}
If you wanted to modify this to pass in a parameter, your header code should declare the message to take a variable of a specific type. Modified, we see that it would look like this, also from my appDelegate header file:- (IBAction) addNewServiceRequest:(id) sender;
Here we identify a method to be implemented in the .m file which takes one variable, called "sender" of type id. This is the implementation of this method, also from my appDelegate:-(IBAction)addNewServiceRequest:(id)sender{
//record the currPoint in here
iLenexaModel *myModel = [iLenexaModel sharedModel];
myModel.currLocation = myModel.lenMapView.mapView.gps.currentPoint;
iLenexaServiceRequestDetail *srRequest = [[iLenexaServiceRequestDetail alloc] initWithNibName:@"iLenexaServiceRequestDetail" bundle:[NSBundle mainBundle]];
[self.tabbarcontroller presentModalViewController:srRequest animated:YES];
[srRequest release];
}
By denoting it as a method of type IBAction, we are in effect tying it to a button in the nib.Edit: You are NOT tying the method to the nib just by typing your return value as IBAction; you have to go into InterfaceBuilder and connect the event to the method. The cool thing about InterfaceBuilder is that it will automatically recognize methods that the event can by bound to.