在UIScrollView中调用UIWebView

问题描述:

我试图通过在主视图控制器中调用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

更改这条线在你的代码,它的工作:

第一,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; 
}