创建单独的UIwebViews

时间:2015-08-12 16:53:28

标签: ios objective-c uiwebview xcode6

我有两个UIwebViews。我编写了每个代码以转到不同的网页网址。但是他们都转到了第一个网址(http://test.bithumor.co/test26.php

这是来自视图控制器的代码.m

//    
//  ViewController.m    
//  BitHumor    
//    
//  Created by danny rodriguez on 7/26/15.    
//  Copyright (c) 2015 BitDeveloping. All rights reserved.    
//    

#import "ViewController.h"



@interface ViewController ()

@property (strong, nonatomic) IBOutlet UIWebView *webView;

@property (weak, nonatomic) IBOutlet UIWebView *webView2;




@end



@implementation ViewController

- (void)viewDidLoad {


    [super viewDidLoad];

    UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];


    NSString *url=@"http://test.bithumor.co/test26.php";

    NSURL *nsurl=[NSURL URLWithString:url];

    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];

    [webview loadRequest:nsrequest];

    [self.view addSubview:webview];

    // Do any additional setup after loading the view, typically from a nib.
    UIWebView *webview2=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    NSString *url2=@"http://google.com";
    NSURL *nsurl2=[NSURL URLWithString:url2];
    NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
    [webview2 loadRequest:nsrequest2];

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

这两个网页视图都与@property相关联,如何让每个网页视图都转到指定的网页网址? (请逐步告诉我,因为我是Objective-C编码的新手)

1 个答案:

答案 0 :(得分:0)

您似乎已经为IBOutlets创建了UIWebViews,请确保将其定义为weak,您将其中一个定义为Strong,将其他一个定义为weak },你所犯的错误是创建了你添加为UIWebView的第三个subview。这将覆盖您的两个uiwebviews上方的整个视图。

     //Not required, remove this code 
     UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
     [self.view addSubview:webview]; 

现在只需创建两个请求并将其加载到UIWebViews

      //For webview 1
      NSString *url=@"http://test.bithumor.co/test26.php";
      NSURL *nsurl=[NSURL URLWithString:url];
      NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
      [self.webView loadRequest:nsrequest];

      //For webview 2
      NSString *url2=@"http://google.com";
      NSURL *nsurl2=[NSURL URLWithString:url2];
      NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
      [self.webView2 loadRequest:nsrequest2];