ChrisC
ChrisC

Reputation: 942

UICollectionViewController: [UICollectionViewController loadView] loaded the "identifier" nib but didn't get a UICollectionView

I want to use a UICollectionViewController in my app, for displaying photos.

I derive a class from UICollectionViewController as:

#import <UIKit/UIKit.h>

@interface AlbumCollectionViewController : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView * cview;


@end

And the implementation is:

#import "AlbumCollectionViewController.h"

@interface AlbumCollectionViewController ()

//@property (weak, nonatomic) IBOutlet UICollectionView * cview;

@end

@implementation AlbumCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;

    // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete method implementation -- Return the number of sections
    //return 0;
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete method implementation -- Return the number of items in the section
    NSUInteger i = 1;
    return i;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    // Configure the cell

    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

}
*/

@end

When loading this view controller, I get

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] loaded the "Lyo-Nd-2MQ-view-KhW-kE-Ben" nib but didn't get a UICollectionView.'

On the main storyboard file, I have a UICollectionViewController whose class and storyboard ID are set to 'AlbumCollectionViewController'.

If I need to set up delegate and datasource connections, how can I do this in code? Or can it only be done on the storyboard file.

Also, if that would not seem to be what is causing this, what else can I do to fix this?

Thanks!

Chris

Upvotes: 2

Views: 7719

Answers (4)

ErasmoOliveira
ErasmoOliveira

Reputation: 1426

Take a look at your storyboard. Then check Settings. I believe you forgot to set the the cell id.

Upvotes: 1

zimmryan
zimmryan

Reputation: 1099

It reads as if you are mixing up Storyboards and Nibs and using initWithNibName: when you should be using instantiateViewControllerWithIdentifier:

The error you are getting is from loading a nib file. Yet you say you have created the view in a Storyboard. If you were using storyboards and the app wasn't able to find the CollectionView the error would read

NSInvalidArgumentException', reason: 'Storyboard () doesn't contain a view controller with identifier 'AlbumCollectionViewController'

To fix it if you are using a .storyboard file with a UICollectionViewController of type AlbumCollectionViewController inside it, instantiate with

UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"NameOfYourStoryboard" bundle:nil];
AlbumCollectionViewController* collectionVC = [sb instantiateViewControllerWithIdentifier:@"AlbumCollectionViewController"];

If you are in fact using .nib file to hold your UICollectionViewController, instantiate with

AlbumCollectionViewController* collectionVC = [[AlbumCollectionViewController alloc] initWithNibName:@"NameOfYourNib" bundle:nil];

Upvotes: 0

Kujey
Kujey

Reputation: 1122

You don't need an IBoutlet cView in your CollectionViewController class. There is already property called .collectionview implemented by the framework.

Get rid of that, and make sure that your collection view is properly linked to its view controller in the storyboard.

Upvotes: 1

Saif
Saif

Reputation: 2968

Make sure that you assign delegates and datasource to the collection view, and you also need to assign flow layout to collection view. And you can access the collection view like self.collectionView, you don't need an outlet for collection view as the class itself is subclass of UICollectionViewController

[self.collectionView setDelegate:self];
[self.collectionView setDataSource:self];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[self.collectionView setCollectionViewLayout:flowLayout];

If it still didn't work, Try removing the outlets for collection view in .xib file and again give it to files owner, also do the same with data source and delegate, and make sure that class of file owner is same as collection view controller class

enter image description here

Hope this helps

Upvotes: 0

Related Questions