Atharva Vaidya Atharva Vaidya - 1 year ago 120
iOS Question

UIPageViewController deallocating first view controller after moving to the third?

I'm using UIPageViewController to display 3 viewcontrollers. If I move to the third and then go back to the first view controller, the first view controller is deallocated from memory. How can I fix this while still using UIPageViewController or is it just not the intended use of UIPageViewController?

Answer Source

Keep a strong reference to the UIViewController's either with an array, or individually in your UIPageViewController. Note that when the device runs low on memory, the controllers will unload their views.


class PageViewController: UIPageViewController {
    var viewControllers: [UIViewController]?
    // or
    var viewControllerA: UIViewController?
    var viewControllerB: UIViewController?
    var viewControllerC: UIViewController?


@interface PageViewController: UIPageViewController

@property (nonatomic, strong) NSMutableArray* viewControllers;
// or
@property (nonatomic, strong) UIViewController* viewControllerA;
@property (nonatomic, strong) UIViewController* viewControllerB;
@property (nonatomic, strong) UIViewController* viewControllerC;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download