views:

418

answers:

2

Hey Everyone,

So i'm building a webView into my application to show the contents of a URL that I am passing from a selection in a UITableView. I know the UIWebView is loading content properly because if you hard code say http://www.google.ca into the NSURL then it loads fine, however when I'm passing the URL that I parsed from an RSS feed back from the UITableView it won't load the URL properly. I tried the debugger and the URL is coming out as nil right before I try and parse it, however I can use NSLog to print the value of it out to the console.

here's the code in my UIViewController that has my UIWebView

 #import <UIKit/UIKit.h>


@interface ReadFeedWebViewController : UIViewController 
{
    NSString *urlToGet;
    IBOutlet UIWebView *webView;
}

@property(nonatomic, retain) IBOutlet UIWebView *webView;
@property(nonatomic, retain) NSString *urlToGet;

@end

Here's the code for my implementation's viewDidLoad method...

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
    [super viewDidLoad];

    NSLog(@"Url inside Web View Controller - %@", urlToGet);
    NSURL *url = [NSURL URLWithString:urlToGet];

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [self.webView loadRequest:requestObj];
}

Once again, I can print the URL to NSLog fine and if I hard code the URL into the NSURL object then it will load fine in the UIWebView. Here is where I'm setting the value in my UITableViewController...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    ReadFeedWebViewController *extendedView = [[ReadFeedWebViewController alloc] init];

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    extendedView.urlToGet = [[stories objectAtIndex: storyIndex] objectForKey:@"link"];
    //NSLog([[stories objectAtIndex: storyIndex] objectForKey:@"summary"]);
    NSLog([[stories objectAtIndex: storyIndex] objectForKey:@"link"]);

    [self.navigationController pushViewController:extendedView animated:YES];

    [extendedView release];
}

However, since I can print the value using NSLog in the extendedView view controller I know it's being passed properly. Cheers

+4  A: 

Hi,

The urlToGet is null into your ReadFeedWebViewController in the viewDidLoad method because when this method is call this variable is not yet affected. The viewDidLoad is call when the initialisation of the ViewController is finished. I should you to call it in the viewWillAppear method like that:

- (void)viewWillAppear:(BOOL)animated {
    NSURL *url = [NSURL URLWithString:urlToGet];

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [self.webView loadRequest:requestObj];
}
Yannick L.
+1  A: 

Also, there is a memory leak in your code. You should release extendedView after pushing it on the navigation controller.

ReadFeedWebViewController *extendedView = [ReadFeedWebViewController new];
if (extendedView != nil) {
    ... setup extendedView here ...
    [self.navigationController pushViewController:extendedView animated:YES];
    [extendedView release];
}
St3fan