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

时间:2011-05-04 10:44:29

标签: iphone uitableview uiwebview viewcontroller navigationcontroller

这个网站的新手,但是在过去几个小时尝试修复我知道必须非常简单的东西后,我的脑袋绝对会让我失望。

短篇小说:

  • 我有一个带UINavigationController的UIViewController
  • UIViewController(tableView)是一个用于解析和显示RSS提要故事的UITableView
  • 第二个UIViewController(newsWebView)带有一个UIWebView,它被UINavigationController推送,用于在用户点击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!

1 个答案:

答案 0 :(得分:3)

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

然后

@synthesize

并在newsWebView.m

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