2013-02-19 65 views
-2

我試圖通過在主視圖控制器中調用UIScrollView來調用在其他視圖(AppleViewController & GoogleViewController)中聲明的兩個UIWebViews。 ScrollView正在顯示,但問題是UIWebView未加載滾動視圖。我的代碼中有什麼問題?在UIScrollView中調用UIWebView

ViewController.h文件

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.scrollView = [[[UIScrollView alloc] init] autorelease]; 
    self.scrollView.delegate = self; 
    [self.view addSubview:self.scrollView]; 
    CGSize scrollViewContentSize = CGSizeMake(640, 404); 
    [self.scrollView setContentSize:scrollViewContentSize]; 
    [self.scrollView setPagingEnabled:YES]; 
    self.scrollView.showsHorizontalScrollIndicator = YES; 
    pageControl = [[[UIPageControl alloc] init] autorelease]; 
    pageControl.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height); 
    pageControl.numberOfPages = 3; 
    pageControl.currentPage = 0; 
    [self.view addSubview:pageControl]; 
    pageControl.backgroundColor = [UIColor blueColor]; 
    AppleViewController *apple=[[AppleViewController alloc]init]; 
    GoogleViewController *google=[[GoogleViewController alloc]init]; 
    [self.scrollView addSubview:apple.view]; 
    [self.scrollView addSubview:google.view]; 

    [scrollView setPagingEnabled:YES]; 
} 

AppleViewController.h

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    webview2=[[UIWebView alloc]initWithFrame:CGRectMake(10, 500,750,350)]; 
    [webview2 setBackgroundColor:[UIColor redColor]]; 
    NSString *[email protected]"http://www.apple.com"; 
    NSURL *nsurl2=[NSURL URLWithString:url2]; 
    NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2]; 
    [webview2 loadRequest:nsrequest2]; 
    [self.view addSubview:webview2]; 
    [webview2 setScalesPageToFit:NO]; 
    webview2.multipleTouchEnabled=YES; 

    [[webview2 layer] setCornerRadius:10]; 
    [webview2 setClipsToBounds:YES]; 
    [[webview2 layer] setBorderColor: 
    [[UIColor blackColor] CGColor]]; 
    [[webview2 layer] setBorderWidth:2.75]; 
    [[self view] addSubview:webview2]; 
    [webview2 release]; 
} 

////GoogleViewController.h

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    webview=[[UIWebView alloc]initWithFrame:CGRectMake(10, 0,750,350)]; 

    [[webview layer] setCornerRadius:10]; 
    [webview setClipsToBounds:YES]; 
    [[webview layer] setBorderColor: 
    [[UIColor blackColor] CGColor]]; 
    [[webview layer] setBorderWidth:3]; 
    [[self view] addSubview:webview]; 
    [webview release];  
    NSString *[email protected]"http://www.google.com"; 
    NSURL *nsurl=[NSURL URLWithString:url]; 
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl]; 
    [webview loadRequest:nsrequest]; 
    [self.view addSubview:webview]; 
    [webview setScalesPageToFit:YES]; 
    webview.multipleTouchEnabled=YES; 
    [webview goBack]; 
    [webview goForward]; 
    webview.opaque = YES; 
    webview.backgroundColor = [UIColor clearColor]; 

} 
+0

爲什麼你要把它放在滾動視圖?中,它會默認滾動。 – Dilip 2013-02-19 12:37:15

+0

這是一個糟糕的設計,把一個'UIWebView'放在'UIScrollView'上,它是'UIScrollView'的子類。你可能想重新考慮你在做什麼。除此之外,我想不出有什麼理由要做這件事。 – Popeye 2013-02-19 12:41:12

+0

請給我一個好的編碼方式,因爲我是ios的新手。 – lreddy 2013-02-19 12:50:08

回答

0

更改這條線在你的代碼,它的工作:

第一,inyour .h文件中

創建您的WebView的財產和編寫代碼這樣

[self.scrollView addSubview:apple.webview2]; 
[self.scrollView addSubview:google.webview]; 

而在你applevc放在initWithNibName方法的網頁視圖代碼,而不是viewDidLoad中方法 相同在第二個vc。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
     webview2=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0,320,640)]; 
     [webview2 setBackgroundColor:[UIColor redColor]]; 
     NSString *[email protected]"http://www.apple.com"; 
     NSURL *nsurl2=[NSURL URLWithString:url2]; 
     NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2]; 
     [webview2 loadRequest:nsrequest2]; 
     [self.view addSubview:webview2]; 
     [webview2 setScalesPageToFit:NO]; 
     webview2.multipleTouchEnabled=YES; 

     [[webview2 layer] setCornerRadius:10]; 
     [webview2 setClipsToBounds:YES]; 
     [[webview2 layer] setBorderColor: 
     [[UIColor blackColor] CGColor]]; 
     [[webview2 layer] setBorderWidth:2.75]; 
     [[self view] addSubview:webview2]; 
    } 
    return self; 
}