Search code examples
iosobjective-cxcodeuiviewcontrollerxcode-storyboard

How to create a Segue on the Storyboard, from a programmatically registered UICollectionViewCell to a new UIViewController?


I'm using Storyboard in Xcode 5.1.1 and I have a UICollectionView there. However, the cells there actually use an XIB (so, not displayed on the Storyboard), which is then programmatically registered to the UICollectionView, like so:

UINib *nibHero = [UINib nibWithNibName:@"MTNNewsListHeroCell"
                            bundle:[NSBundle mainBundle]];    

[self.newsListCollectionView registerNib:nibHero
              forCellWithReuseIdentifier:@"heroCellID"];

So far, so good. Everything is working fine.

Next, I would like to create a segue from this UICollectionViewCell to another UIViewController on the Storyboard. It's a pretty standard "click an item, open the detailed view" feature.

Normally, if the Cell exists on the Storyboard, inside the UICollectionView, I can draw a line on the Storyboard from it to the next UIViewController to create the Segue. However, the Cell does not exist on the Storyboard (again, because the cells are made as an XIB file):

How to make the segue from Cell to the next UIViewController?

The goal is simply to make it so that other programmers who read my code can understand the flow of the app better, using the Storyboard.

Is it possible to show and create a Segue on the Storyboard, from a programmatically registered UICollectionViewCell (made inside a UICollectionView that's a subview of News List View Controller, in my example), to a new UIViewController (the Single News view, in my example), and have that connection actually show up on the Storyboard?

Final update:

This is what I ended up doing, after following the suggestion from user @rdelmar. I think it does the job nicely:

Dummy segue with yellow notes


Solution

  • No, you can't do that. A segue is basically a visual representation of a transition from one view controller to the next. You need both ends of that segue to be visually represented on the storyboard to make one. You could add a cell into your collection view and connect a segue to it just to show the flow in the storyboard, but it would never be invoked because the cell you actually have in the collection view will have been created from the xib file (I'm not sure that would make things clearer to someone looking at your code though). If you really want an actual functioning segue, then make the cell in the storyboard instead.