11

I have a 3 viewcontroller navigation where A presents modal controller B, which presents modal controller C all via segues. C has an unwind segue back to B. It also has an unwind back to A. When I perform action for C to unwind to B, it unwinds but then pops B and goes back to A. This is not what I want, I want it in this case to stay on B. Below are the segues VC C uses.

unwind segues from VC C

unwindCancel is for when user clicks on a collectionViewCell and goes back to VC B. prepareForUnwind is just a standard "cancel" button to VC A.

Below is code for didSelectItem to call the unwind in VC C. Below that is prepareForSegue in VC C.

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:@"unwindCancel" sender:self];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"unwindCancel"]) {
        GalleryDetailViewController *detailVC = segue.destinationViewController;
        detailVC.colletionCount = self.indexPathToPass;
    }
}

VC B unwind in .m file

-(IBAction)unwindCancel:(UIStoryboardSegue *)segue{

    [self.collectionView scrollToItemAtIndexPath:self.colletionCount atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}

VC A unwind in .m file

-(IBAction)prepareForUnwind:(UIStoryboardSegue *)segue {
}
noobsmcgoobs
  • 2,596
  • 5
  • 26
  • 48

2 Answers2

3

When going from C to B, don't use an unwind segue just have C call dismissViewController. If you're committed to using unwind segues, look here specifically section entitle How an Unwind Segue Determines its Destination View Controller

itsji10dra
  • 4,550
  • 3
  • 36
  • 55
beyowulf
  • 14,133
  • 2
  • 29
  • 36
2

I guess you confused an unwind-segue's identifier with unwind-segue's Action method.

If you build an unwind-segue with "prepareForUnwind" action, and then you change this unwind-segue's identifier to "unwindCancel".The problem would appear.

Just make sure the unwind-segue's identifier matches it's action method.

wj2061
  • 6,201
  • 1
  • 28
  • 60
  • 1
    There are 2 unwind segues from C - one back to the B and one back to A. – noobsmcgoobs Nov 05 '15 at 03:40
  • 1
    @noobsmcgoobs I recreated your demo,the problem does not exist.So I guess you give the segue a wrong name.Put "nslog" in your two unwind methodes, and see which one is called. – wj2061 Nov 05 '15 at 03:53
  • @noobsmcgoobs This Post is similar to your :http://stackoverflow.com/q/33181577/4975761 – wj2061 Nov 06 '15 at 16:48
  • The name's not wrong but VC C pops off back to A using `prepareForUnwind` as well. When I dragged the segue from the VC, not the button, to Exit, this doesn't happen. I'll try the solution posted, but on first glance it's not a naming error like the solution. – noobsmcgoobs Nov 07 '15 at 09:15