0

So the problem I'm having is that I have a CollectionView that loads different data depending on the index. If, for example, I'm at index 0 and the first cell is "hat" and I switch to index 1 and the first cell is "coat". The problem is if I click on the "coat" and then click the Back button... it shows the coat but we're really still on "hat". And if you click on the "coat" it will bring up the details for the "hat".

I solved a similar problem before by just reloading the CollectionView upon pressing the back button... but that doesn't seem to be working this time. So what I want to do is make sure the segmented control index is set to 0 before the reload.

Here's what I have so far from the Hat/Coat detailview:

UserViewController *userVC = [[UserViewController alloc] init];
[userVC.collectionView reloadData];

And here's the code for the segmented control in the UserViewController:

HMSegmentedControl * segmentedControl = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, 280, 600, 50)];
[segmentedControl setSelectedSegmentIndex:0];

I can't quite figure out the code to change that index from the detail view. Or, if that wouldn't work, to just get it to reload the whole view.

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
mystic cola
  • 1,465
  • 1
  • 21
  • 39

1 Answers1

0

You can use NSUserDefaults.

In AppDelegate :

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

if ([userDefaults objectForKey:@"segmentedControlIndex"] == nil) {
    [userDefaults setInteger:0 forKey:@"segmentedControlIndex"];
}

In viewWillAppear function :

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[self.segmentedControl setSelectedSegmentIndex:[userDefaults integerForKey:@"segmentedControlIndex"]];

And on switch :

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults setInteger:self.segmentedControl.selectedSegmentIndex forKey:@"segmentedControlIndex"];
Pipiks
  • 2,018
  • 12
  • 27
  • Not working. First it won't let me do self.segmentedControl because it says that object is not found on type UserView. So if I load it as shown above in my question and then do segmentedControl instead of self.segmentedControl... then it doesn't show the error... but it also doesn't work. – mystic cola Dec 18 '15 at 13:07