0

Even though I am setting delegate and datasource, the data source methods are never being called.

I have a ViewController that adds a subview as such:

EVPhotoCollectionViewController *pc = [[EVPhotoCollectionViewController alloc] initWithNibName:@"EVPhotoCollectionViewController" bundle:nil];
self.damagePhotosView = pc.view;

Inside EVPhotoCollectionViewController I have delegate and datasource wired up in the xib, but also via code as follows:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    [self.collectionView reloadData];
}

None of the datasource methods are ever called. I have verified self.collectionView is not null when it calls reloadData.

Thanks!

skwashua
  • 1,627
  • 17
  • 14

1 Answers1

2

I think there are several things wrong here--and there are some complexities to view controller containment that you may need to read up on.

First off, you're not adding the EVPhotoCollectionViewController view as a subview of your vc, eg:

[self addSubView:pc.view];

Also, you're not setting a frame for the EVPhotoCollectionViewController, so depending on how it's implemented, it might not show up with the right size/position.

Lastly, it doesn't look like you're retaining the EVPhotoCollectionViewController anywhere. Its view will be retained by the view hierarchy, but it looks like the instance of EVPhotoCollectionViewController will be dealloc'd once the function creating it goes out of scope.

View controller containment: How does View Controller Containment work in iOS 5?

Community
  • 1
  • 1
Nicholas Hart
  • 1,734
  • 13
  • 22
  • You got it. Once I added the view as a subView it gave a bad access error (already dealloc'd). Thanks! – skwashua Jul 15 '13 at 18:53
  • Cool! I would still recommend checking out the view controller containment article. There are some tricks to getting it work properly and some weird bugs could crop up if not done (eg: the child VC won't get its viewWill/DidAppear called). – Nicholas Hart Jul 15 '13 at 19:10