4

我如何使用三个 webview 来显示多个 html 页面,比如在滚动视图中显示 50 个。任何示例代码请参考我。我目前的代码是这样的。它会导致内存泄漏,我当然知道这是一个愚蠢的问题。我坚持这个超过2天。请帮我。

-(void)AddWebviews
{
    [m_CtrlActivity stopAnimating];
    m_CtrlLblProgress.hidden=YES;
    int divisor = WebViewAddCount % 3;
    MagazineAppDelegate     *appdelegate              = (MagazineAppDelegate *)[[UIApplication sharedApplication]delegate];

    IstructPageDetails *objPageDetails=[appdelegate.m_mutarrPageDetails objectAtIndex:WebViewAddCount];
    if (divisor==0)
    {
         m_CtrlWebViewone=[[UIWebView alloc]initWithFrame:CGRectMake(768*WebViewAddCount,0,768,1024)];
        // m_CtrlWebView.scrollView.delegate=self;
        // m_CtrlWebView.delegate=self;
        NSString *m_strMagazineFolder=[[GlobalFunctions GetCachesFolder]stringByAppendingPathComponent:MAGAZINE_FOLDER_NAME];
        NSString *finalpath=[NSString stringWithFormat:@"%@/%@/%@/%@",m_strMagazineFolder,appdelegate.m_StrSelectedMagazineUrl,objPageDetails.m_strFolderName, objPageDetails.m_strPageName ];
        NSURL *url = [NSURL fileURLWithPath:finalpath isDirectory:NO];

        [m_CtrlWebViewone loadRequest:[NSURLRequest requestWithURL:url]];

        m_CtrlWebViewone.scrollView.pagingEnabled=YES;
        m_CtrlWebViewone.scrollView.bounces=NO;
        m_CtrlWebViewone.scalesPageToFit=YES;
        m_CtrlWebViewone.backgroundColor=[UIColor clearColor];


        self.m_CtrlScrollview.pagingEnabled=YES;
        [self.m_CtrlScrollview addSubview:m_CtrlWebViewone];
        [m_CtrlWebViewone release];

    }
    else if(divisor==1)
    {
         m_CtrlWebViewtwo=[[UIWebView alloc]initWithFrame:CGRectMake(768*WebViewAddCount,0,768,1024)];
        // m_CtrlWebView.scrollView.delegate=self;
        // m_CtrlWebView.delegate=self;
        NSString *m_strMagazineFolder=[[GlobalFunctions GetCachesFolder]stringByAppendingPathComponent:MAGAZINE_FOLDER_NAME];
        NSString *finalpath=[NSString stringWithFormat:@"%@/%@/%@/%@",m_strMagazineFolder,appdelegate.m_StrSelectedMagazineUrl,objPageDetails.m_strFolderName, objPageDetails.m_strPageName ];
        NSURL *url = [NSURL fileURLWithPath:finalpath isDirectory:NO];

        [m_CtrlWebViewtwo loadRequest:[NSURLRequest requestWithURL:url]];

        m_CtrlWebViewtwo.scrollView.pagingEnabled=YES;
        m_CtrlWebViewtwo.scrollView.bounces=NO;
        m_CtrlWebViewtwo.scalesPageToFit=YES;
        m_CtrlWebViewtwo.backgroundColor=[UIColor clearColor];

        self.m_CtrlScrollview.pagingEnabled=YES;
        [self.m_CtrlScrollview addSubview:m_CtrlWebViewtwo];
        [m_CtrlWebViewtwo release];
    }
    else if(divisor==2)
    {
         m_CtrlWebViewthree=[[UIWebView alloc]initWithFrame:CGRectMake(768*WebViewAddCount,0,768,1024)];
        // m_CtrlWebView.scrollView.delegate=self;
        // m_CtrlWebView.delegate=self;
        NSString *m_strMagazineFolder=[[GlobalFunctions GetCachesFolder]stringByAppendingPathComponent:MAGAZINE_FOLDER_NAME];
        NSString *finalpath=[NSString stringWithFormat:@"%@/%@/%@/%@",m_strMagazineFolder,appdelegate.m_StrSelectedMagazineUrl,objPageDetails.m_strFolderName, objPageDetails.m_strPageName ];
        NSURL *url = [NSURL fileURLWithPath:finalpath isDirectory:NO];

        [m_CtrlWebViewthree loadRequest:[NSURLRequest requestWithURL:url]];

        m_CtrlWebViewthree.scrollView.pagingEnabled=YES;
        m_CtrlWebViewthree.scrollView.bounces=NO;
        m_CtrlWebViewthree.scalesPageToFit=YES;
        m_CtrlWebViewthree.backgroundColor=[UIColor clearColor];

        self.m_CtrlScrollview.pagingEnabled=YES;
        [self.m_CtrlScrollview addSubview:m_CtrlWebViewthree];
        [m_CtrlWebViewthree release];
    }

    WebViewAddCount++;


    [self.m_CtrlScrollview setContentSize:CGSizeMake(WebViewAddCount*768, 1004)];
}
4

2 回答 2

0

UIWebView 类参考中所述:

重要提示:您不应该在 UIScrollView 对象中嵌入 UIWebView 或 UITableView 对象。如果这样做,可能会导致意外行为,因为两个对象的触摸事件可能会混淆并错误处理

于 2013-01-12T10:59:19.287 回答
0

一次在滚动视图上添加多个 webview 不是一个好主意,这可能会导致内存泄漏,然后应用程序崩溃。所以最好的方法是在必要时加载 webview。我建议你有两个选项......

1) Apple 有一个名为UIPageviewController的框架。您只需将转换类型 UIPageViewControllerTransitionStylePageCurl 控件更改为 UIPageViewControllerTransitionStylePageSwipe。

2)使用贝克框架

ARC 和非 ARC 代码均可用。

希望它可以帮助你:)

于 2013-01-12T12:28:47.227 回答