问题描述
我设置 UIPageViewController
来前进和后退一些视图控制器。我有一个问题,当视图控制器添加到 UIPageViewController
时,内存使用量将增加,直到编译器给我收到内存警告
然后应用程序运行得太慢,直到应用程序清空内存并再次正常工作。如何在不增加内存的情况下浏览视图控制器?或者更好地说当新的on添加时如何删除当前视图控制器。这是我的代码:
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];
前进和后退行动:
- (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;
}
推荐答案
回答你的问题问题
-
如何在不增加内存的情况下浏览视图控制器?:
"How can I navigate through view controllers without increasing memory?":
你真的不能这样做。加载新的视图控制器时,它将加载所有视图,视图控制器存储在内存中。您无法加载和显示不在内存中的视图控制器。
You can't really do that. When you load a new view controller it will load all of it views and the view controller is stored in memory. You can't load and display a view controller that is not in memory.
当新的on添加时,如何删除当前视图控制器:
"How can remove current view controller when a new on adds":
你不应该这样做。空视图控制器没有很大的内存占用,您可以在导航堆栈中轻松拥有20多个视图控制器而不会出现任何内存问题。虽然可以实现这样的东西,但它需要做很多工作,并且它无法解决问题的根源。
You shouldn't do that. An empty view controller doesn't have a big memory footprint, you can easily have 20+ view controllers in the navigation stack without any memory problem. Although it is possible to implement something like this, it takes a lot of work, and it doesn't solve the root of your problem.
那么问题是什么?
您遇到了内存管理问题。它不是由您发布的代码引起的,看起来很好。问题必须出在代码的另一部分。
You have a memory management problem. It is not caused by the code you have posted, that looks fine. The problem must be in another part of your code.
有两种常见情况可能导致内存管理问题:
There are two common cases that could cause memory management issues:
-
保留周期:
保留周期基本上是两个对象相互保留的时间。这违反了对象所有权的标准规则,使两个对象无权释放另一个,导致内存泄漏(数字是保留计数):
Retain cycles:A retain cycle is essentially when two objects retain each other. This goes against the standard rules of object ownership, leaving both objects with no authority to release the other, causing a memory leak (the numbers are the retain count):
不必要的缓存:如果您正在下载大量不同的图像并对其进行缓存,那么这不是缓存的理想用例。缓存是存储频繁访问的对象的理想选择。如果您不经常访问这些图像或收到didReceiveMemoryWarning消息,则应释放这些对象。
Unnecessary caching: If you are downloading a lot of different images and caching them, that is not the ideal use case for caching. Caching is ideal for storing frequently accessed objects. If you are not frequently accessing these images or receive a didReceiveMemoryWarning message, you should release these objects.
如何调试内存问题
第一个也是最简单的事情是覆盖视图控制器的dealloc方法。覆盖viewcontroller上的dealloc方法将有助于确保在您预期的时候释放视图控件。
The first and most simple thing to do is to override the view controller's dealloc method. Overriding the dealloc method on a viewcontroller will help ensure that a viewcontroller is being deallocated when you expect it to be.
-(void)dealloc {
NSLog(@"viewcontroller is being deallocated");
}
如果这没有帮助,你可以尝试隔离问题并调试它使用Xcode Instruments。 文章可能是一个很好的开始。
If this doesn't help you can try to isolate the problem and debugging it with Xcode Instruments. This article could be a good start for you.
这篇关于UIPageViewController并删除当前视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!