创建单独的 UIwebView

Create Separate UIwebViews

我有两个 UIwebView。我对每个代码进行编码以转到不同的网页 url。但是他们都去了第一个 url (http://test.bithumor.co/test26.php)

这是视图中的代码 controller.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

两个网页视图都链接到@属性,如何让它们都转到指定的网页url? (请逐步告诉我,因为我是 Objective-C 编码新手)

看起来您已经为 UIWebViews 创建了 IBOutlets,确保将它们定义为 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];