Xcode上有多个UIwebViews

时间:2015-07-28 00:35:13

标签: ios objective-c xcode uiwebview

我有大约+3个UIwebViews,如何对每个视频进行编码以转到不同的URL。

这是我的ViewController.m文件中的代码

 #import "ViewController.h"
    @interface ViewController ()


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


    @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/test22.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.
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }



    @end

如何为每个UIwebView保留相同的代码,但每个UIwebView可以转到不同的网页?

示例:
UIwebView1 = http://example.com/1
UIwebView2 = http://example.com/2
UIwebView1 = http://example.com/3

1 个答案:

答案 0 :(得分:2)

您有两个选择,您可以使用URL&amp ;;创建三个UIWebView实例。请求每个请求,或者您可以重复使用相同的请求,并继续重新分配它的URL请求,然后再次调用它。

如果你想创建三个独立的UIWebView实例,你只需要做两次以上,但是将每一个添加到当前视图只会让你看到最后一个。

UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    NSString *url=@"http://test.bithumor.co/test22.php";
    NSURL *nsurl=[NSURL URLWithString:url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [webview loadRequest:nsrequest];

UIWebView *webview2=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    NSString *url2=@"http://nextUrl";
    NSURL *nsurl2=[NSURL URLWithString:url2];
    NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
    [webview2 loadRequest:nsrequest2];

UIWebView *webview3=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    NSString *url3=@"http://anotherNexturl";
    NSURL *nsurl3=[NSURL URLWithString:url3];
    NSURLRequest *nsrequest3=[NSURLRequest requestWithURL:nsurl3];
    [webview3 loadRequest:nsrequest3];

或者,根据您在Web视图之间切换的方式,您可以重新分配NSURLRequest并重新使用UIWebView的相同实例

UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    NSString *url=@"http://test.bithumor.co/test22.php";
    NSURL *nsurl=[NSURL URLWithString:url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [webview loadRequest:nsrequest];

    NSString *url2=@"http://nextUrl";
    NSURL *nsurl2=[NSURL URLWithString:url2];
    NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
    [webview loadRequest:nsrequest2];

    NSString *url3=@"http://nextUrl";
    NSURL *nsurl3=[NSURL URLWithString:url3];
    NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl3];
    [webview loadRequest:nsrequest3];