In my project I'm using a UIPageControl
as a container UIViewController
for scrolling. I used this tutorial (using ARC
and storyboards): http://www.wannabegeek.com/?p=168 and the source code: https://github.com/wannabegeek/PageViewController
As you can see there are 3 ViewControllers
in the project, and they are added as child on the CustomPagerViewController
. In that project there are only 3 ViewControllers
that are added, but in my project I got more than 3 ViewControllers
and I also reuse them with another text, image, label, etc. on it. The problem is that in the project all those ViewControllers
are getting loaded whenever the CustomPagerViewController
is loaded and this costs memory so I'm looking for another way how I can deal with this problem instead of loading them all at once?

- 20,427
- 11
- 57
- 70
-
see this link might help you. http://stackoverflow.com/questions/2942636/how-can-i-change-the-color-of-pagination-dots-of-uipagecontrol – Tirth Apr 01 '13 at 11:27
-
I'm searching for a way how I can put the view controller on the UIPageControl without adding them all as child since this takes much memory... – Apr 01 '13 at 11:55
-
Before I add an answer: Do you use images on all three views, if so how big are they? What does Instruments say about All Allocations in your implementation of the app. – Bill Thompson May 09 '13 at 19:13
-
`UIPageControl` is simply a control that shows which page you are on, it does not actually manage a scroll view. – Greg May 11 '13 at 05:52
-
The technique I use for large (or infinite) scroll views to avoid loading all pages at once is to create a scroll view with a size that will fit 3 pages, then, when the user swipes left or right to change pages, as soon as the animation finishes, the scroll views are shifted left or right, along with the viewpoint, such that the current scroll view is once again in the centre of the scroll view, and one of the previous scroll views is removed, and another one is added on the other side with new content. – Greg May 11 '13 at 05:54
1 Answers
You shouldn't try to optimise controller allocation in this way. Obviously retaining view controllers in memory takes up space, but believe me, not that much.
We have to difference here between the controller and its view. Views can take up a lot of memory in order to get themselves displayed on screen, but iOS already has its own mechanisms to free this memory when it's no longer required (ie: the view is not on screen / doesn't have a window).
iOS view controllers used to free views memory by themselves (this behaviour was in [UIViewControllers didReceiveMemoryWarning]), but that's no longer the case in iOS 6. Now you're responsible for doing so in you feel like it's needed by your app. Bear in mind that despite nilling these views in this method (or dealloc), you won't be saving much memory, as most part of the (graphical) resources used to display a view in screen may have already been released by iOS, and the amount of memory you may end up freeing close to 0.
To sum up, in your case I'd convert PagerViewController into a proper iOS container controller using this guide. The key is to call the following methods:
[UIViewController addChildViewController:]
[UIViewController willMoveToParentViewController]
[UIViewController removeFromParentViewController]
[UIView addSubview:]
[UIView removeFromSuperview]
in the right order according to your needs. In your case, you can add / remove these in the scrollViewDidScroll method. Use them in this way, and let Apple's magic happen.
You might as well use UIPageViewController which gives you some nice out-of-the-box features.

- 2,069
- 13
- 25