iOS - UITableView 在不同的视图控制器中将 URL 推送到 UIWebView
我是这个网站的新手,但在过去的几个小时里尝试修复一些我知道必须非常简单的东西后,我的头简直要死了。
简短的故事:
- 我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 newsWebView 类中有一个
NSUrl
类型的实例变量urlObject
。确保添加@property
和@synthesize
对象。然后
在 newsWebView.m 中
Have an instance variable say
urlObject
ofNSUrl
type in your newsWebView class. Make sure you add@property
and@synthesize
the object.then
And in newsWebView.m