UIWebView memory management

后端 未结 4 988
轮回少年
轮回少年 2020-12-15 19:19

I have a problem with memory management.

I am developing an application that makes heavy use of UIWebView. This app generates dynamically lots of UIWebViews while lo

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-15 19:54

    If you add multiple subviews within the same instance of a viewcontroller, you must remove the old subview from the superview before you add a new subview.

    Add a tag to the UIWebView before you add it as a subview:

    webView.tag  = 10;
    [self.view addSubview:webView];
    
    webView = nil;
    

    Before you add the new webview(this will clear the old view), remove the subview from the superview:

    UIWebView * oldWebView = (UIWebView *)[self.view viewWithTag:10];
    [oldWebView removeFromSuperview];
    

    If you don´t remove the old webviews from the superview, they will build up in memory until you release the viewcontroller.

提交回复
热议问题