在UIScrollView中调用UIWebView

时间:2013-02-19 12:26:34

标签: objective-c uiwebview uiscrollview

我正在尝试通过在主视图控制器中调用UIWebViews来调用在其他视图(AppleViewController & GoogleViewController)中声明的两个UIScrollView。 ScrollView正在显示,但问题是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 *url2=@"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 *url=@"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];

}

1 个答案:

答案 0 :(得分:0)

在代码中更改此行,它将起作用:

首先,在你的.h文件中创建webview的属性

并像这样编写这段代码

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

在你的applevc中将你的webview代码放在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 *url2=@"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;
}