3
votes

Explanation: I have two viewcontrollers in my project. (Both of their class names are - ViewController) Each view controller have one uiwebview.

When I test the app, the first uiwebview opens the url webpage of the second uiwebview. (The second uiwebview url is http://google.com and the first one is http://test.bithumor.co/test26.php, so the first uiwebview opens http://google.com)

Here's the code from the .m file

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

How do I fix it so each uiwebviews opens their designated url.

First webview - http://test.bithumor.co/test26.php

Second webview - http://google.com

ADDITIONAL: Some are saying i have two outlets in one ViewController, where is that and how do I fix it so one webview is on each (2) viewcontroller.

2
FYI - Do not delete and repost your questions. That's bad form. If you have more info, you should update the previous question.rmaddy
Your explanation is wrong. You have posted one view controller that contains two web views.rmaddy
@rmaddy, how, I don't understand. It's TWO view controller with one webview on EACH. Please show me where it says two on one viewcontroller.PHP Web Dev 101
It's right there in the code you posted. You have two web view defined as properties in this one view controller class. The viewDidLoad method creates and add BOTH web views and add them both to this ONE view controller.rmaddy

2 Answers

1
votes

What rmaddy is saying is that what you wrote as your explanation and what the code shows are not the same. I understand you say you have two view controllers, but you only show the code for one. You are essentially creating one view that sits directly on top of another and has the same dimensions. You will never see the web view underneath this way. If you do something like this you should see both webviews, one on top of the other. That will show you that there really is two there, but one was just hidden behind the other:

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

Edit 1:

If you really want two different view controllers, you need to do something like this:

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

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

If you are using storyboards or nibs, then you would want to set each view controller to a different class. One will get set to ViewController, the other to ViewController2

Edit 2:

To do this in one file you could do something like this:

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

File where you present your web view controller:

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

Or if it's presented via segue you can do set the urlString property in the prepareForSegue method

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

Based on your code, there two possible scenarios: First: simply what your calling 2 ViewControllers is actually 1 ViewController with two views, if this is the case add functionality to bring the one you one to front. Second: you actually have two controllers. if your you are using the code you attached for both ViewControllers, then you will always end up with having the second web view in front of the first one. Both are loaded, but you can see the upper one.

I hope this helped in identifying the issue.