I'm working with a model class and passing it back and forth between 3 controllers. I'm having trouble allowing customers to make selections on one controller and have them displayed in string form within a UILabel on another controller.
Each time the customer is taken to the controller where they make the selection and returned back to the previous controller, their selection is shown in string form as expected but previous selections are cleared. So I'm only able to show the selection for one category at a time.
Please read on for further details:
Controller 1 - The page that displays a collection view of clothing items
Controller 2 - A page the customer is taken to when tapping refine
Controller 3 - The page the customer selects the ways they wish to refine
In Controller 2 I display a list of different ways to refine search results by:
In Controller 3 the customer can make the actual selection:
I'm working with a class file which acts as my model. It's class is called VAGRefineModel. This class is first initialised in controller 1 and during the prepareForSegue method set as the value of a property of controller 2 called refineModel.
In controller 2 I pass a copy of the model to controller 3 using the prepareForSegue method again:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[[sender titleLabel] text] isEqualToString:@"Gender"]) {
VAGRefineGenderViewController *vc = [segue destinationViewController];
[vc setDelegate:self];
[vc setRefineModel:[[self refineModel] copy]];
}
}
Once customer lands on controller 3 they check what ever boxes are appropriate. Then by tapping the done button or tapping back the details are saved in the copy of the model. I use NSUserDefaults to track the status of my switches. This isn't important right now. The issue I'm having is after the customer returns to controller 2 their checkbox selection is displayed underneath the refine by option they chose. In my example you see underneath gender my selection is shown.
Ok this is fine but now when I tap on let's say brand to choose some more ways to refine my results then return back to controller 2 selection for gender is cleared and blank. I assume because the prepareForSegue method creates a new copy of my model to set as controller 3's refineModel property value.
I need a way to temporarily keep track of the selections made before the apply button is tapped. The apply button saves my copy model as the original models value. This is in place so that if a customer decides they don't wish to apply the selections they have made and return to controller 1 without tapping apply the original or previous selections if any, are returned.
More info:
I need a clear way to keep hold of all the data selected in controller 3. The way my app is set up it just gets reset.
Here is a delegate method from controller 3 that the model from controller 3 is passed to controller to in.
- (void)didTapDoneButtonWithRefineModel:(VAGRefineModel *)refineModel
{
[self setRefineModel: refineModel];
}
I take the model passed over from controller 3 and set controller 2's property value to that. However this is the original I am setting. Nothing is saved unless apply button is tapped.
I have a UILabel under gender, brand, product type etc. This get's set upon returning to controller 2 from controller 3 depending on what's been selected.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self chosenGenders] setText:[[self refineModel] selectedGenderString]];
[[self chosenBrands] setText:[[self refineModel] selectedBrandString]];
}
Question:
I need a way to keep those labels set once they have been set. If I return to controller 1 by tapping the back button everything is cleared. If apply button is tapped instead the data is stored in the original model.
How do I keep hold of the selection made in controller 3 no matter what?
Copy method as requested:
#import "VAGRefineModel.h"
@implementation VAGRefineModel
{
VAGRefineModel *_object;
}
-(id)copyWithZone:(NSZone *)zone
{
VAGRefineModel *modelCopy = [[VAGRefineModel alloc] init];
modelCopy->_object = [_object copyWithZone: zone];
return modelCopy;
}