2013-01-12 54 views
4

如何使用三個網頁瀏覽來顯示多個HTML頁面,例如在滾動視圖中顯示50。任何示例代碼請參閱我。我目前的代碼是這樣的。它會導致內存泄漏,當然我知道它是愚蠢的。我堅持了2天以上。請幫幫我。ios中的多個網頁瀏覽

-(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)]; 
} 

回答

1

正如UIWebView Class Reference說:

重要:您不應該嵌入 的UIScrollView對象的UIWebView或UITableView的對象。如果這樣做,意外的行爲可能會導致 因爲兩個物體的觸摸事件可以混合起來,錯誤地 處理

1

在時間上滾動視圖添加多個網頁視圖是不是一個好主意,這可能會導致內存泄漏,然後應用程序崩潰。所以最好的方法是加載webview,只要有必要。有兩個選項,我建議你...

1)蘋果有一個框架,稱爲UIPageviewController。你只需要改變過渡類型UIPageViewControllerTransitionStylePageCurl控制到UIPageViewControllerTransitionStylePageSwipe。

2)使用Bakers framework

ARC和非ARC代碼都可用。

希望它可以幫助你:)

+0

麪包==> https://github.com/germanocaldeira/baker –

+0

這不是OP問:)問題是:「我可以」而不是「我應該」。我不會投票,因爲它是有用的信息,但它不解決這個問題:) –