user737753
user737753

Reputation:

iOS - UITableView Pushing URL to UIWebView in Different View Controller

new to this site, but my head is absolutely killing me after trying for the past few hours to fix something that I know has to be insanely simple.

Short story:

I have everything working except for the push of each row's URL to the webView in the second controller. It pushes the view, but the URL just isn't getting sent to the second controller so the webView is blank. The NSLogs I have in the code indicate that the URL is being created successfully, too, so what do I need to do to get newsWebView to handle the URL?

tableView.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

    NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];

    // clean up the link - get rid of spaces, returns, and tabs...
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"  " withString:@""];

    NSLog(@"storyLink: %@", storyLink);


    if (storyLink != nil && ![storyLink isEqualToString:@""]) {
        // Wrapping the troublesome call with logs so you should be able to see if this is the line that is killing your app
        NSLog(@"about to create url");
        NSURL *url = [NSURL URLWithString:storyLink];
        NSLog(@"creating url was successful");

        //URL Request Object
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

        //Load the request in the UIWebView
        newsWebView *detailViewController = [[newsWebView alloc] initWithNibName:@"newsDetailWebView" bundle:nil];
        //detailViewController.item = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];
        [self.navigationController pushViewController:detailViewController animated:YES];
        //[detailViewController loadRequest:requestObj];
        [detailViewController release];
    }
}

So, what do I need to add to viewDidLoad in newsWebView.m? Or, for that matter, what is wrong with my tableView's didSelectRowAtIndexPath?

newsWebView.m

- (void)viewDidLoad {


    }

Thank you very, very much. I need to take some Advil!

Upvotes: 2

Views: 3308

Answers (1)

visakh7
visakh7

Reputation: 26390

Have an instance variable say urlObject of NSUrl type in your newsWebView class. Make sure you add @property and @synthesize the object.

then

newsWebView *detailViewController = [[newsWebView alloc] initWithNibName:@"newsDetailWebView" bundle:nil];
        newsWebview.urlObject = url;
        [self.navigationController pushViewController:detailViewController animated:YES];
        //[detailViewController loadRequest:requestObj];
        [detailViewController release];

And in newsWebView.m

- (void)viewDidLoad {
[webView loadRequest:[NSURLRequest requestWithURL:urlObject]];
}

Upvotes: 2

Related Questions