Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First uiwebview is showing url of second webview

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.

like image 327
PHP Web Dev 101 Avatar asked Nov 20 '25 14:11

PHP Web Dev 101


1 Answers

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";
   }
}
...
like image 119
JiuJitsuCoder Avatar answered Nov 23 '25 05:11

JiuJitsuCoder