我大约有+3个UIwebViews,如何编码每个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保留相同的确切代码,但如何将每个代码都转到不同的网页?

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

最佳答案

您有两种选择,您可以创建一个UIWebView的三个实例,每个实例都有一个URL和请求,或者您可以重复使用相同的实例,并不断重新设置它的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];

关于ios - Xcode上的多个UIwebViews,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31665613/

10-09 10:13