0

I've used this answer to implement a UIScrollView with paging.

It works great as a start, but what I want to be able to do is for each page in the UIScrollView, I want a separate instance of the same UIView.

For example, say I have ContentViewController, and I want to use 4 instances of it's UIView;

1 for each of the pages on the UIScrollView - how do I go about doing that?

My initial thought was to instantiateViewController(withIdentifier), and then reuse it's .view' property inside thefor` loop, but this only gave me the correct view once, and the other 3 were blank subviews.

Any help would be much appreciated. Thanks!

Community
  • 1
  • 1
nickjf89
  • 458
  • 1
  • 7
  • 18

1 Answers1

1

You could instantiate the ViewController multiple times (in a for loop) and then fetch each controller's view and add it to the scroll view. But you have to be aware that now you also have 4 Controllers, each for one view.

Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34
  • Thanks. Is there a way to avoid having multiple Controllers? – nickjf89 Feb 01 '17 at 11:03
  • It's not that easy, because the controller typically holds outlets to the view (and it's subviews) and reacts on events of controls. If you want to serve multiple views, your controller would have to deal with this, e.g. would have to know which view to fill with which data resp. how to react on which e.g. button pressed on which of the buttons in any of the multiple views. But if you just have simple views (say: only UIImageViews), you could also create them in the calling code, so they will work without the original controller. In this case you would have to provide the data from "outside". – Andreas Oetjen Feb 01 '17 at 11:09
  • Ah yes, that makes perfect sense. Thanks! It works for me. – nickjf89 Feb 01 '17 at 11:11