How do I hook up UICollectionViewController in UIC

2020-04-11 06:41发布

问题:

I have an app that looks like this:

ViewController is the parent VC into which I placed 3 container view controllers.
I subclassed UIViewController for all three, Top, Mid & BottomContainerViewControllers.

Here is my question. I want the BottomVC to have a UICollectionView which scrolls sideways. So I added a UICollectionView as you can see (gave it a nice greenish background).

But my question is, how do I hook this up?

This is my BottomContainerViewController.h:

#import <UIKit/UIKit.h>

@interface BottomContainerViewController : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) IBOutlet UICollectionView *collectionView;

@end

And the .m file has the following methods:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

But when I run it I get:

[UICollectionViewController loadView] loaded the "qcP-Hl-Txn-view-aJq-Dc-875" nib but didn't get a UICollectionView.'

The UICollectionView outlet is connected. What else could be the problem?

回答1:

Create a uiviewcontroller class to manage the bottom container. Link as you normally would using that view controller. Connect delegate, datasource, implement the delegate and data source... etc...



回答2:

I ran into the same issue, this is what worked for me:
1: Click the "Show Document Outline" button in the bottom left of your storyboard.
2: Find "Bottom Container View" which should be a subclass of UICollectionViewController
3: Cut the subviews of the highest level view (Everything you see from expanding the first view)
4: Delete that primary view and paste back in your collection view

Essentially, the top view has to be a UICollectionView and not the usual UIView that is defaulted by putting the Container into the storyboard.