iProgrammed
iProgrammed

Reputation: 980

Load Webview in different view in iOS Programming

I have been programming an Web browser app for iOS and stumbled upon an issue. At first my search text field and web view where in the same view (Everything was fine :). When I put the web view in an different view the web view wouldn't load up the page and stay blank. So the issue is the web view will not load in a different view (will work if text field and web view are in the same view).

My Code:

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize searchButton;




- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib

}


-(IBAction)SearchButton:(id)sender {
    NSString *query = [searchField.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/search?q=%@", query]];
                                       NSURLRequest *request = [NSURLRequest requestWithURL:url];
                                       [webView loadRequest:request];

    ViewController *WebView = [self.storyboard instantiateViewControllerWithIdentifier:@"WebView"];
    [self presentViewController:WebView animated:YES completion:nil];
  }


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Upvotes: 0

Views: 436

Answers (1)

danh
danh

Reputation: 62686

The SearchButton method refers to a webView in the current class, but it's the new ViewController (you call it WebView) which is about to be presented and that should contain a UIWebView. So the UIWebView on the presented view controller is never getting to loadRequest.

Create a subclass of UIViewController to contain your web view. (call it something like MyWebViewController) It should have a property called urlString. Make sure to change the class of the view controller you currently have painted in storyboard to MyWebViewController.

The SearchButton method should look like this:

// renamed to follow convention
- (IBAction)pressedSearchButton:(id)sender {

    NSString *query = [searchField.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSString *urlString = [NSString stringWithFormat:@"http://www.google.com/search?q=%@", query];

    // remember to change the view controller class in storyboard
    MyWebViewController *webViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WebView"];

    // urlString is a public property on MyWebViewController
    webViewController.urlString = urlString;
    [self presentViewController:webViewController animated:YES completion:nil];
}

The new class can form the request from the urlString...

// MyWebViewController.h
@property (nonatomic, strong) NSString *urlString;
@property (nonatomic, weak) IBOutlet UIWebView *webView;  // be sure to attach in storyboard

// MyWebViewController.m

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    NSURL *url = [NSURL URLWithString:self.urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

Upvotes: 1

Related Questions