-2

我试图通过调用主视图控制器来调用UIWebViews在其他视图中声明的两个。ScrollView 正在显示,但问题是未加载到滚动视图。我的代码有什么问题?(AppleViewController & GoogleViewController)UIScrollViewUIWebView

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];

}
4

1 回答 1

0

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

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

并像这样编写此代码

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

并在您的 applevc 中将您的 webview 代码放入 initWithNibName 方法中,而不是在第二个 vc 中相同的 viewdidload 方法中。

- (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;
}
于 2013-02-19T12:49:09.390 回答