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?
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.
PrepareForSegue
will never be called if you set shouldPerformSegueWithIdentifier:NO
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With