Im using a very large UIScrollView with webviews/subviews etc, so I need it to be dynamic, and preloaded with 3 container views. It works in the simulator, but crashes on the device with low memory. I am adding and removing from the 3 master views using a recursive method
private void destroyChildren(UIView view, int level) {
if (view.Subviews.Length > 0) {
foreach (UIView subview in view.Subviews) {
destroyChildren(subview, level++); // recurse
subview.RemoveFromSuperview();
if (subview is UIWebView) {
(subview as UIWebView).ShouldStartLoad -= webHandler;
}
if (subview is scrollViewer) {
(subview as scrollViewer).clicked -= viewClicked;
}
subview.BackgroundColor = UIColor.Cyan; // release bgImage?
subview.Dispose();
}
}
else {
if (level> 0) { // dont dispose root view
view.Dispose();
}
}
and called with destroyChildren(myView, 0); The App still crashes after 10 or so page turns with memory warnings. Is this code okay, or even necessary? Should I use some kind of viewcontroller开发者_如何学JAVA instead.
Thanks!!!
From what I know and I hope I am correct about this when you call subview.Dispose() it is not garbage collected immediately but later after some other app usage. Try to set subviews content to null where possible and than call Dispose(). Also consider implementing DidReceiveMemoryWarning method inside your subviews/master views. I am using a similar app using up to three scrollviews each with four pages and it works with no problems.
精彩评论