iOS - UITableView 在不同的视图控制器中将 URL 推送到 UIWebView

发布于 2024-11-04 20:10:53 字数 2311 浏览 0 评论 0原文

我是这个网站的新手,但在过去的几个小时里尝试修复一些我知道必须非常简单的东西后,我的头简直要死了。

简短的故事:

  • 我有一个带有 UINavigationController 的 UIViewController
  • UIViewController (tableView) 是一个 UITableView,用于解析和显示 RSS feed 故事
  • 还有第二个 UIViewController (newsWebView) 和一个由 UINavigationController 推送的 UIWebView,旨在将每个故事显示为用户单击 tableView 中的
  • 行当用户完成 UIWebView 时,它们会被推回到第一个视图,tableView

除了将每行的 URL 推送到第二个控制器中的 webView 之外,一切都正常。它推送视图,但 URL 没有发送到第二个控制器,因此 webView 是空白的。我在代码中的 NSLogs 表明 URL 也已成功创建,那么我需要做什么才能让 newsWebView 处理该 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];
    }
}

那么,我需要在 newsWebView.m 中的 viewDidLoad 中添加什么?或者,就此而言,我的 tableView 的 didSelectRowAtIndexPath 有什么问题?

newsWebView.m

- (void)viewDidLoad {


    }

非常非常感谢。我需要吃一些Advil!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘东忘西忘不掉你 2024-11-11 20:10:53

在 newsWebView 类中有一个 NSUrl 类型的实例变量 urlObject。确保添加 @property@synthesize 对象。

然后

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

在 newsWebView.m 中

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

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]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文