UIPageViewController and removing current view controller

烂漫一生 提交于 2019-12-05 09:40:46

To answer your questions

  1. "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.

  2. "How can remove current view controller when a new on adds":

    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.

What is the problem then?

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:

  1. 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):

  2. 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.

How can you debug memory issues

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");
 }

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.

To avoid memory and manage view controllers you need to stack all view controllers in a container.

So that every next/previous call will be made on that container and it will provide as your need. Also one more benefit of this container is to avoid reinitialise controller again and again.

Once you add view controller in container you are no need to alloc init or initialise that controller second time.

It's a big code to paste here and also storyboard setup will not be seen. So i share you one demo to visualise and use page controller with container to hold view controllers.

PageDemo

I don't see any memory issue in your code. Try using profiler tool called "Leaks" and try to find what object did not released from memory. There is official Apple guide for that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!