我是这个网站的新手,但在过去的几个小时里尝试修复一些我知道必须非常简单的东西后,我的脑子简直要死了。
短篇故事:
- 我有一个带有 UINavigationController 的 UIViewController
- UIViewController (tableView) 是一个解析并显示 RSS feed 故事的 UITableView
- 还有第二个 UIViewController (newsWebView),其 UIWebView 由 UINavigationController 推送,旨在当用户单击 tableView 中的行时显示每个故事
- 当用户使用完 UIWebView 后,他们会被推回到第一个 View tableView
除了将每行的 URL 推送到第二个 Controller 中的 webView 之外,一切正常。它推送 View ,但 URL 没有发送到第二个 Controller ,因此 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 {
}
非常非常感谢。我需要服用一些止痛药!
最佳答案
在 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]];
}
关于iphone - iOS - UITableView 在不同的 View Controller 中将 URL 推送到 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5882182/