UIPageViewController and removing current view controller

I setup

UIPageViewController
to forward and backward through some view controllers . I have a problem which is when a view controller adds to
UIPageViewController
the memory usage will increase until the compiler gives me
Received memory warning
and then app runs too slowly until the app empties the memory and works fine again . How can I navigate through view controllers without increasing memory ? or better to say how can remove current view controller when a new on adds . here is my code :


// Create the page view controller.
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;


//disabling tap and swipe guesture
for (UIGestureRecognizer *recognizer in self.pageViewController.gestureRecognizers) {
    recognizer.enabled = NO;
}

// Instantiate the first view controller.
UIViewController *startingViewController = [self viewControllerAtIndex:0];

[self.pageViewController setViewControllers:@[startingViewController]
                                  direction:UIPageViewControllerNavigationDirectionForward
                                   animated:NO
                                 completion:^(BOOL finished) {
                                     // Completion code
                                 }];

// Add the page view controller to this root view controller.
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];


Forward and Backward actions :

- (void)goToPreviousContentViewController
{


    // Get index of current view controller
    UIViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
    NSString *vcRestorationID = currentViewController.restorationIdentifier;
    NSUInteger index = [self.contentPageRestorationIDs indexOfObject:vcRestorationID];


    UIViewController *previousViewController = [self viewControllerAtIndex:index - 1];


    [self.pageViewController setViewControllers:@[previousViewController]
                                      direction:UIPageViewControllerNavigationDirectionReverse
                                       animated:NO
                                     completion:^(BOOL finished) {
                                         // Completion code
                                     }];
}



More Codes :

#pragma mark - UIPageViewControllerDataSource
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return self.contentPageRestorationIDs.count;
}




- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSString *vcRestorationID = viewController.restorationIdentifier;
    NSUInteger index = [self.contentPageRestorationIDs indexOfObject:vcRestorationID];


    if (index == 0) {
        return nil;
    }


    return [self viewControllerAtIndex:index - 1];
}


- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSString *vcRestorationID = viewController.restorationIdentifier;
    NSUInteger index = [self.contentPageRestorationIDs indexOfObject:vcRestorationID];


    if (index == self.contentPageRestorationIDs.count - 1) {
        return nil;
    }


    return [self viewControllerAtIndex:index + 1];
}

I setup UIPageViewController to forward and backward through some view controllers . I have a problem which is when a view controller adds to UIPageViewController the memory usage will increase until the compiler gives me Received memory warning and then app runs too slowly until the app empties the memory and works fine again . How can I navigate through view controllers without increasing memory ? or better to say how can remove current view controller when a new on adds . here is my code : // Create the page view controller. self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"]; self.pageViewController.dataSource = self; //disabling tap and swipe guesture for (UIGestureRecognizer *recognizer in self.pageViewController.gestureRecognizers) { recognizer.enabled = NO; } // Instantiate the first view controller. UIViewController *startingViewController = [self viewControllerAtIndex:0]; [self.pageViewController setViewControllers:@[startingViewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:^(BOOL finished) { // Completion code }]; // Add the page view controller to this root view controller. [self addChildViewController:self.pageViewController]; [self.view addSubview:self.pageViewController.view]; [self.pageViewController didMoveToParentViewController:self]; Forward and Backward actions : - (void)goToPreviousContentViewController { // Get index of current view controller UIViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0]; NSString *vcRestorationID = currentViewController.restorationIdentifier; NSUInteger index = [self.contentPageRestorationIDs indexOfObject:vcRestorationID]; UIViewController *previousViewController = [self viewControllerAtIndex:index - 1]; [self.pageViewController setViewControllers:@[previousViewController] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:^(BOOL finished) { // Completion code }]; } More Codes : #pragma mark - UIPageViewControllerDataSource - (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController { return self.contentPageRestorationIDs.count; } - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { NSString *vcRestorationID = viewController.restorationIdentifier; NSUInteger index = [self.contentPageRestorationIDs indexOfObject:vcRestorationID]; if (index == 0) { return nil; } return [self viewControllerAtIndex:index - 1]; } - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { NSString *vcRestorationID = viewController.restorationIdentifier; NSUInteger index = [self.contentPageRestorationIDs indexOfObject:vcRestorationID]; if (index == self.contentPageRestorationIDs.count - 1) { return nil; } return [self viewControllerAtIndex:index + 1]; }

UIPageViewController and removing current view controller
 
 
Q