gpt4 book ai didi

ios - 第一个 uiwebview 显示第二个 webview 的 url

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:05 24 4
gpt4 key购买 nike

说明: 我的项目中有两个 View Controller 。 (它们的类名都是-ViewController)每个 View Controller 都有一个uiwebview。

当我测试应用程序时,第一个 uiwebview 打开第二个 uiwebview 的 url 网页。 (第二个 uiwebview url 是 http://google.com 而第一个是 http://test.bithumor.co/test26.php ,所以第一个 uiwebview 打开 http://google.com )

这是 .m 文件中的代码

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) IBOutlet UIWebView *webView2;

@end

@implementation ViewController

@synthesize scrollView;

- (void)viewDidLoad
{
[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
draw1 = 0;
scrollView.frame = CGRectMake(0, 300, 480, 55);
[scrollView setContentSize:CGSizeMake(480, 55)];

openMenu.frame = CGRectMake(220, 270, 60, 30);


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];

webview.scrollView.bounces = NO;

[self.view addSubview:webview];
[self.view bringSubviewToFront:webview];
[self.view bringSubviewToFront: openMenu];
[self.view bringSubviewToFront: scrollView];


// 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];

webview2.scrollView.bounces = NO;

[self.view addSubview:webview2];
[self.view bringSubviewToFront:webview2];
[self.view bringSubviewToFront: openMenu];
[self.view bringSubviewToFront: scrollView];

如何修复它以便每个 uiwebviews 打开它们指定的 url。

第一个 WebView - http://test.bithumor.co/test26.php

第二个 WebView - http://google.com

附加:有人说我在一个 ViewController 中有两个 socket ,它在哪里以及我该如何修复它以便每个 (2) 个 View Controller 上都有一个 webview。

最佳答案

rmaddy 的意思是你写的解释和代码显示的不一样。我知道你说你有两个 View Controller ,但你只显示了一个的代码。您实质上是在创建一个直接位于另一个 View 之上且具有相同尺寸的 View 。你永远不会以这种方式看到下面的 WebView 。如果你这样做,你应该看到两个 webviews,一个在另一个之上。这将向您表明那里确实有两个,但一个只是隐藏在另一个后面:

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) IBOutlet UIWebView *webView2;

@end

@implementation ViewController

@synthesize scrollView;

- (void)viewDidLoad
{
[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
draw1 = 0;
scrollView.frame = CGRectMake(0, 300, 480, 55);
[scrollView setContentSize:CGSizeMake(480, 55)];

openMenu.frame = CGRectMake(220, 270, 60, 30);


UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height/2)];

NSString *url=@"http://test.bithumor.co/test26.php";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];

[webview loadRequest:nsrequest];

webview.scrollView.bounces = NO;

[self.view addSubview:webview];
[self.view bringSubviewToFront:webview];
[self.view bringSubviewToFront: openMenu];
[self.view bringSubviewToFront: scrollView];


// Do any additional setup after loading the view, typically from a nib.
UIWebView *webview2=[[UIWebView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width,self.view.frame.size.height/2)];
NSString *url2=@"http://google.com";
NSURL *nsurl2=[NSURL URLWithString:url2];

NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];

[webview2 loadRequest:nsrequest2];

webview2.scrollView.bounces = NO;

[self.view addSubview:webview2];
[self.view bringSubviewToFront:webview2];
[self.view bringSubviewToFront: openMenu];
[self.view bringSubviewToFront: scrollView];

编辑 1:

如果你真的想要两个不同的 View Controller ,你需要做这样的事情:

文件 1:

#import "ViewController.h"

@interface ViewController ()

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

@end

@implementation ViewController

@synthesize scrollView;

- (void)viewDidLoad
{
[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
draw1 = 0;
scrollView.frame = CGRectMake(0, 300, 480, 55);
[scrollView setContentSize:CGSizeMake(480, 55)];

openMenu.frame = CGRectMake(220, 270, 60, 30);


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];

webview.scrollView.bounces = NO;

[self.view addSubview:webview];
[self.view bringSubviewToFront:webview];
[self.view bringSubviewToFront: openMenu];
[self.view bringSubviewToFront: scrollView];

文件2:

#import "ViewController2.h"

@interface ViewController2 ()

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

@end

@implementation ViewController2

@synthesize scrollView;

- (void)viewDidLoad
{
[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
draw1 = 0;
scrollView.frame = CGRectMake(0, 300, 480, 55);
[scrollView setContentSize:CGSizeMake(480, 55)];

openMenu.frame = CGRectMake(220, 270, 60, 30);


UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];

NSString *url=@"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];

[webview loadRequest:nsrequest];

webview.scrollView.bounces = NO;

[self.view addSubview:webview];
[self.view bringSubviewToFront:webview];
[self.view bringSubviewToFront: openMenu];
[self.view bringSubviewToFront: scrollView];

如果您正在使用 Storyboard或 nib,那么您可能希望将每个 View Controller 设置为不同的类。一个将设置为 ViewController,另一个设置为 ViewController2

编辑 2:

要在一个文件中执行此操作,您可以执行以下操作:

ViewController.h:

...
@interface ViewController : UIViewController
@property (strong, nonatomic) NSString *urlString;
@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

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

@end

@implementation ViewController

@synthesize scrollView;

- (void)viewDidLoad
{
[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
draw1 = 0;
scrollView.frame = CGRectMake(0, 300, 480, 55);
[scrollView setContentSize:CGSizeMake(480, 55)];

openMenu.frame = CGRectMake(220, 270, 60, 30);


UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];

NSURL *nsurl=[NSURL URLWithString:self.urlString];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];

[webview loadRequest:nsrequest];

webview.scrollView.bounces = NO;

[self.view addSubview:webview];
[self.view bringSubviewToFront:webview];
[self.view bringSubviewToFront: openMenu];
[self.view bringSubviewToFront: scrollView];

你展示你的 WebView Controller 的文件:

...
ViewController *webViewController = [[ViewController alloc] init];
webViewController.urlString = @"http://www.google.com";
[self presentViewController:webViewController animated:YES completion:nil];

或者如果它是通过 segue 呈现的,你可以在 prepareForSegue 方法中设置 urlString 属性

...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"yourSegueIDHere"]) {
ViewController *webViewController = (ViewController *)segue.destinationViewController;
webViewController.urlString = @"http://www.google.com";
}
}
...

关于ios - 第一个 uiwebview 显示第二个 webview 的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32229716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com