0

I have a UIView (it's actually a PM::Screen) and am adding a subView which is a UICollectionViewController's view. I'm using this technique to switch between things like grid/list ala the Instagram profile screen.

Selecting a cell should open a new screen, which I want to push on to the parent view's (the PM::Screen) navigation controller.

My question is how do I get the collectionView(view, didSelectItemAtIndexPath: index_path) to call a method on the parent PM screen?

Will I have to use NSNotificationCenter?

Chris Edwards
  • 3,514
  • 2
  • 33
  • 40
  • This isn't a solution, but... are you trying to push from the selected cell itself? That seems dangerous due to cell reuse -- might be tricky to ensure a cell doesn't have a stale reference to the containing view. – Anna Dickinson Dec 28 '14 at 18:12
  • I'm passing data from the selected cell to a method in the parent, which then does the pushing – Chris Edwards Dec 28 '14 at 18:46
  • 1
    So, can't the collectionView just have a reference to the parent ViewController and use it to push? I.e. Call partentViewController.navigationController.pushViewController(...) – Anna Dickinson Dec 28 '14 at 19:38
  • parentViewController gave me errors, is there anything wrong with the way I'm doing it now? – Chris Edwards Dec 28 '14 at 19:40
  • 1
    Use protocols for passing data to parent – Adeel Ur Rehman Dec 28 '14 at 19:51
  • @AnnaDickinson or are you saying I should specify what `parentViewController` is? – Chris Edwards Dec 28 '14 at 20:03
  • 1
    Oh, sorry, yes -- I didn't realize "parentViewController" was an existing property, and I don't know if it's set to the correct thing automatically. I meant -- in general -- give the collectionView a reference to its parent when you create it. And as @AdeelUrRehman mentions, it might be better to define a delegate protocol which allows the collectionView to notify its parent when the push needs to happen rather than doing the push itself -- that's the usual design pattern. – Anna Dickinson Dec 28 '14 at 20:23

1 Answers1

0

I have managed with:

self.view.superview.nextResponder.theMethodToCall

Is there a better way?

UPDATE:

I ended up passing a reference to the parent to the child, and using that reference to call the method on the parent

Chris Edwards
  • 3,514
  • 2
  • 33
  • 40