Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prepareForSegue never being called

I have a UICollectionView consisting of numerous cells. I'd like to be able to tap on one of these cells and have my Storyboard segue to another view controller after determining that this is the appropriate action.

I have created my secondary view controller, along with the segue, in my Storyboard. In my UICollectionView subclass, I've implemented the following...

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    return NO; // So that I can determine whether or not to perform the segue based on app logic
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];

    // Selection logic here
    [self performSegueWithIdentifier:@"showDetailView" sender:self];
}

In this class I also have implemented -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender to allow me to setup my detail view prior to the segue being performed. It looks like this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   NSLog(@"HELLO, WORLD!!!");
}

Everything seems to be in order, but I am finding that prepareForSegue is NEVER called. I've tried logging, setting breakpoints. There is no indication that this method is EVER called. What am I missing here? Why is this method not being called? While it has a simple "Hello, World" statement now, I previously had code that my detail view depended on and it would result in an exception because the detail view was not setup properly.

Is there something else I'm missing here? Something I should be doing that I am not currently?

like image 240
Shadowman Avatar asked Oct 24 '13 19:10

Shadowman


2 Answers

You are telling iOS to never segue with this:

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    return NO; // So that I can determine whether or not to perform the segue based on app logic
}

Change NO to YES and it should work. Or add logic inside that at least returns YES when you want the segue.

like image 157
David H Avatar answered Sep 28 '22 17:09

David H


PrepareForSegue will never be called if you set shouldPerformSegueWithIdentifier:NO.

like image 29
Daniel Larsson Avatar answered Sep 28 '22 17:09

Daniel Larsson