首先uiwebview显示第二个webview的网址

时间:2015-08-26 14:42:53

标签: ios objective-c uiwebview

说明:我的项目中有两个viewcontrollers。 (它们的两个类名都是--ViewController)每个视图控制器都有一个uiwebview。

当我测试应用程序时,第一个uiwebview打开第二个uiwebview的url网页。 (第二个uiwebview网址为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打开指定的网址。

第一次网络视图 - http://test.bithumor.co/test26.php

第二次网页浏览 - http://google.com

附加:有些人说我在一个ViewController中有两个出口,在那里是如何修复它,所以每个(2)视图控制器都有一个webview。

2 个答案:

答案 0 :(得分:1)

rmaddy所说的是你所写的作为你的解释以及代码所显示的内容并不相同。我知道你说你有两个视图控制器,但你只显示一个代码。您实际上是在创建一个直接位于另一个视图之上且具有相同尺寸的视图。您永远不会在这种方式下看到Web视图。如果你这样做,你应该看到两个webview,一个在另一个上面。这将告诉你那里确实有两个,但其中一个隐藏在另一个之后:

#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:

如果你真的想要两个不同的视图控制器,你需要做这样的事情:

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

如果您使用的是故事板或笔尖,那么您可能希望将每个视图控制器设置为不同的类。一个将设置为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];

显示Web视图控制器的文件:

...
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";
   }
}
...

答案 1 :(得分:0)

根据您的代码,有两种可能的情况: 第一:简单地说你的调用2 ViewControllers实际上是一个具有两个视图的ViewController,如果是这样的话,添加功能可以将你的一个带到前面。 第二:你实际上有两个控制器。如果您使用的是为两个ViewControllers附加的代码,那么您将始终在第一个Web视图前面放置第二个Web视图。两者都已加载,但您可以看到上面的一个。

我希望这有助于确定问题。