Encoding data in an instance of one class and decoding the data in an instance of a different class

StackOverflow https://stackoverflow.com/questions/22467322

  •  16-06-2023
  •  | 
  •  

Question

I am new to Objective-C and Cocoa Touch. I have two view controllers, one an instance of one class and the second an instance of a different class. Using I have encoded data used in the second view controller. I would like to set the data to nil from the first view controller. Is that possible and if so how? Attached is code to save the data in the second view controller. I have no code to decode the data in the first controller.

- (void)saveImages
{
    NSMutableData *data = [[NSMutableData alloc]init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver         alloc]initForWritingWithMutableData:data];

if(self.finalFaceImage != nil)
{
    [archiver encodeObject:self.finalFaceImage forKey:@"face"];
}
if(self.finalEyesImage != nil)
{
    [archiver encodeObject:self.finalEyesImage forKey:@"eyes"];
}
if(self.finalNoseImage != nil)
{
    [archiver encodeObject:self.finalNoseImage forKey:@"nose"];
}
if(self.finalLipsImage != nil)
{
    [archiver encodeObject:self.finalLipsImage forKey:@"lips"];
}

[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];

}

Was it helpful?

Solution

It is a good idea to adopt the model-view-controller approach to application design. Separate your application's data (model) into its own class. You can then instantiate this class and pass the reference between your view controllers.

One good approach to doing this is to instantiate the model in your appDelegate (say in didFinishLaunchingWithOptions:) and store it in a property of your appDelegate. Then, whenever you need to access the data you can obtain a reference by using something like [UIApplication sharedApplication].delegate.myData

Your saveImages method (and a corresponding loadImages method if required) would also move into your model class, although it may still be invoked by your viewController.

OTHER TIPS

If the first viewcontroller creates the second as a child viewcontroller then it can keep a reference to it and call a method on it to nil the data. See this documentation about using child viewcontrollers:

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

Otherwise, if the first creates the second before dismissing itself it can also call a method on it before transitioning to it.

There are a number of ways of dealing with this depending on your architecture.

Hope this helps.

This is a perfect case for the MVC (Model View Controller) design pattern. The model is the data storage object in MVC.

Create a data container object that has properties to hold the data you need to share.

Give both your view controllers a pointer to this object so they can read/write data to it as needed.

Then implement NSCoding in that object. When your object conforms to NSCoding, you can use the NSKeyedArchiver method archivedDataWithRootObject: to archive the entire object into an NSData block with a single call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top