【问题标题】:Multiple UIwebViews on XcodeXcode 上的多个 UIwebViews
【发布时间】:2015-07-28 00:35:12
【问题描述】:

我有大约 +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

如何为每个 UIwebViews 保留相同的确切代码,但让每个 UIwebViews 访问不同的网页?

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

【问题讨论】:

    标签: ios objective-c xcode uiwebview


    【解决方案1】:

    您有两个选择,您可以创建三个 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];
    

    【讨论】:

    • 能否提供代码,我是Objective-C的初学者
    • 已更新,这只是一个示例,您可以根据自己的使用方式以及如何在要加载的不同 URL 之间切换。
    • 我是否必须对实际的 UIwebViews 做任何事情
    • 在一个示例中,您重新使用相同的 Web 视图,而在另一个示例中,您创建了 3 个 UIWebView 实例,我不知道您的确切目标是什么,但如果您使用 3 个实例,您将如果这是您的目标,则必须管理它们向用户显示的方式。
    • 昨晚我尝试了几个小时来实现它,但是无论我尝试了多少次,都没有代码起作用:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多