我设置了UIPageViewController
以在一些视图控制器之间前进和后退。我的问题是,当一个视图控制器添加到UIPageViewController
时,内存使用量会增加,直到编译器给出Received memory warning
,然后应用程序运行缓慢,直到应用程序清空内存并再次正常工作。如何在不增加内存的情况下导航视图控制器?或者更好地说,当添加新的视图控制器时,如何删除当前视图控制器?以下是我的代码:
// 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];
前进和后退操作:
- (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
}];
}
更多代码:
#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];
}
#pragma mark - Private Methods
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
// Only process a valid index request.
if (index >= self.contentPageRestorationIDs.count) {
return nil;
}
// Create a new view controller.
BaseContentViewController *contentViewController = (BaseContentViewController *)[self.storyboard instantiateViewControllerWithIdentifier:self.contentPageRestorationIDs[index]];
// Set any data needed by the VC here
contentViewController.rootViewController = self;
return contentViewController;
}