开发者

iOS - UITableView Pushing URL to UIWebView in Different View Controller

开发者 https://www.devze.com 2023-03-02 13:53 出处:网络
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.

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 a UIViewController with a UINavigationController
  • The UIViewController (tableView) is a UITableView that parses and displays RSS feed stories
  • There is a second UIViewController (newsWebView) with a UIWebView that gets pushed by the UINavigationController and is meant to display each story as the user clicks on rows in tableView
  • When the user is done with the UIWebView, they are pushed back to the first view, tableView

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!


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号