iOS: Storyboard CollectionView segue not being triggered

独自空忆成欢 提交于 2019-12-04 03:20:10

You cannot create segues directly from cells in a storyboard because the collectionview is populated dynamically through the data source. You should use the collectionView:didSelectItemAtIndexPath: and perform the segue programatically using performSegueWithIdentifier:sender:. Something like this:

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

where MySegueIdentifier is the identifier of the segue defined in storyboard.

Luke

TLDR: FOR A STORYBOARD, do not call registerClass:forCellWithReuseIdentifier:. It overrides what the storyboard sets up for the cell (including how segues are handled): How to set a UILabel in UICollectionViewCell

Brief setup

  • Used a storyboard
  • Created a new collection view controller using the Xcode template, setting it as a subclass of UICollectionViewController.
  • Initially used the default UICollectionViewCell, adding a UILabel programmatically.

The generated UICollectionViewController code registered the cell in viewDidLoad:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

First Issue: The prepareForSegue:sender: event was not firing, which brought me to this answer . I implemented the UICollectionViewDelegate and collectionView:didSelectItemAtIndexPath: event, then called the segue programmatically. This fixed my first issue.

Second Issue: I switched to a custom cell containing one label. After hooking everything up, the cell label was not displaying. After some digging, I found a solution contained in the link at the top of my answer.

Third Issue and Solution: I removed the registerClass:forCellWithReuseIdentifier: line. When I ran my app, the label appeared correctly, but when I tapped a cell, it called the prepareForSegue:sender event twice. By removing the registerClass:forCellWithReuseIdentifier line, the cell was processing cell touches directly, without the need of the delegate method. This is how I expected the storyboard to work. I deleted the collectionView:didSelectItemAtIndexPath: event, which resolved the double-firing of prepareForSegue:sender:. If you are using a storyboard, do not register the cell class. It overwrites what storyboard sets up.

Have you made your CollectionView Cell's connection in Triggered Segues on selection?

You can also trigger a segue programatically using [self performSegueWithIdentifier:@"segueIdentifier" sender:nil]

in

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

Equivalent Swift code for similar question.

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier(@"TargetSegway", sender: self)
} 

Make sure, in case if your cell has other overlapping views, "User Interaction Enabled" is unchecked (you can find this option, under attribute inspector View/Interaction). Otherwise, your Tap Gesture is consumed by the overlapping view, didSelectItemAtIndexPath may not be called.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!