5
votes

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?

3

3 Answers

3
votes

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

6
votes

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.

2
votes

Be sure if you are using a prototype cell (with Dynamic Cells) that you set the identifier in the storyboard and use it in your delegate as well. I had this issue and took me a bit to figure it out.

static NSString *cellIdent = @"prototypeCellName";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];