I have an object called Layer that has some attributes and some methodes.
i need to pass Layer to a second view controller:
SecondVC *view = [self.storyboard instantiateViewControllerWithIdentifier:@"2VC"];
view.Layer = [[Layer alloc] initWithMapLayer:self.Layer];
view.delegate = self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:view];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:navController animated:YES];
in the SecondVC, i may change the attributes. I then return the Layer object back via a delegate;
-(void)done
{
[self.delegate returnLayer:self.layer];
[self dismissModalViewControllerAnimated:YES];
}
now my problem is that i am passing a pointer to my first view controller's Layer object and when i update the Layer in the second view controller, my first view controller's Layer object is also being updated.
because of this, i can't tell if it has been changed (if it has, i need to run some code).
how can i create a copy of my Layer object and pass that instead of a pointer to my first view controller's Layer object?
EDIT:
i did try use a second init method:
-(id)initWithLayer:(Layer *)Layer
{
if (self = [super init])
{
self.call = [[FunctionCall alloc] init];
self.HUD = [[MBProgressHUD alloc] init];
self.Layers = [[NSMutableDictionary dictionaryWithDictionary:Layer.Layers] copy];
self.nameList = [[NSArray arrayWithArray:Layer.nameList] copy];
}
return self;
}
which did not work out.
EDIT2:
tried
Layer *copyLayer = [self.myLayer copy];
layerView.myLayer = copyLayer;
and got error
-[layer copyWithZone:]: unrecognized selector sent to instance 0xfc72c40
2012-06-12 11:15:28.584 Landscout[8866:1fb07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Layer copyWithZone:]: unrecognized selector sent to instance 0xfc72c40'
SOLVED:
i added a deep copy to the initWithLayer method:
for (id key in layer.layers)
{
[newLayers setValue:[[layer.layers objectForKey:key] mutableCopy] forKey:[key mutableCopy]];
}
for (id name in layer.nameList)
{
[newList addObject:[name mutableCopy]];
}
this gives me a copy of the Layer object