ios - Xcode 上的多个 UIwebView

标签 ios objective-c xcode uiwebview

我有大约 +3 个 UIwebView,如何对每个 UIwebView 进行编码以转到不同的 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

最佳答案

您有两种选择,您可以创建 UIWebView 的三个实例,每个实例都有一个 URL 和请求,或者您可以重复使用同一个实例,只是继续重新分配它的 URL 请求,然后再次调用它。

如果你想创建三个独立的 UIWebView 实例,除了两次以上,你将只做你拥有的,尽管将每个实例添加到当前 View 只会让你看到最后一个实例。

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 View 之间切换的方式,您可以重新分配 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 上的多个 UIwebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665613/

相关文章:

ios - 在 iOS 中将应用程序注册为默认的 pdf 开启器

objective-c - UIDeviceOrientationDidChangeNotification 与 UIApplicationDidChangeStatusBarOrientationNotification

ios - OS X 中的accessibilityElementsHidden?如何保护我的应用程序的 UI 内部结构不被窃取?

python - xCode 上的 Python 代码辅助

ios - 在没有 Apple 开发者帐户的情况下分发 iOS 应用程序

ios - 代码不解除键盘

ios - Swift:编辑从相机拍摄的图像以填充 UIImageView

objective-c - 用于在墙上张贴的自定义控件(如在 Facebook 应用程序中)

objective-c - iPhone 屏幕截图以供查看

iphone - UIStoryboardSegue 和 WebView