当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - What is the proper way to remove a UIViewController from the background memory when using instantiateViewControllerWithIdentifier?

问题描述:

I am presenting a view controller like this:

UINavigationController * PlacesNC = [storyboard instantiateViewControllerWithIdentifier:@"PlacesNavigationController"];

[self presentViewController:PlacesNC animated:YES completion:nil];

Once I am at the final view controller that is presented using the navigation view controller, how can I remove the initial view controller from memory?

Edit

In my application it is possible to present multiple UIViewControllers in this fashion. I will try to give a representation of what my stack might look like.

____ modal vc 5 * current view controller

____ modal vc 4

____ modal vc 3

____ modal vc 2

____ modal vc 1

____ root view controller

So if I have a stack like this, how can I remove vc 1,2,3 and maybe even 4 so that I can release the memory these other view controllers are using up? This may not be the correct to allow such a stack as this, but this is what I have now, and I just need a temporary fix as the iPhone 4 cannot handle the amount of memory used in a stack like this. So until I can come up with an alternate way to present my view controllers I just need to be able to remove some of them from the stack.

网友答案:

You cannot, and you don't want to. It is still in the view controller hierarchy, as I show in this diagram (DrillViewController is presented, but RootViewController is still there, "behind" it):

Presenting a view controller does not destroy the presented view controller, nor should it; if it did, you would not be able to dismiss the presented view controller and find the presenting view controller still sitting there.

Moreover, in iOS 8 it is perfectly possible to present a view controller and show the presented view controller's view in front of the presenting view controller's view, which remains visible behind it. That, for example, is how a UIAlertController presentation works. Clearly it would be a disaster if the presenting view controller ceased to exist in that situation.

Now, it may be that what you really mean is: My view controller has a property that uses a lot of memory. I don't need to hang on to that when my view controller is not frontmost. So I'd like to release it when I present another view controller. In that case, just manage the memory manually: set that property to nil on viewDidDisappear:, and recover its value (somehow) on viewWillAppear: when the presented view controller is dismissed.

One final suggestion: Perhaps the real problem is that a presented view controller was just the wrong type of hierarchical arrangement to start with. Perhaps what you really want to do is replace your original view controller in the view controller with the new view controller - because you are never coming back to it and don't need it any longer. That is perfectly possible, but of course you'll need to set up a different hierarchy architecture.

分享给朋友:
您可能感兴趣的文章:
随机阅读: