我正在开发一个从 rss 提要中获取内容的新闻应用程序。当我单击 TableViewCell 时,我将带有标题、链接等的 NSDictionary 对象传递给下一个 ViewController。在 ViewController 我定义 NSDictionary *item; 我可以通过像这样设置视图控制器的标题来验证值是否正确传递: self.title = [item objectForKey:@"link"]; 标题显示了我试图在我的 UIWebView 中打开的链接。
这是我的 ViewController 的实现
//
// NewsDetailViewController.m
// NewsApp2
//
// Created by Marco Soria on 12/27/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "NewsDetailViewController.h"
@implementation NewsDetailViewController
@synthesize item, itemTitle, itemDate, itemSummary;
-(id)initWithItem:(NSDictionary *)theItem{
if(self = [super initWithNibName:@"NewsDetail" bundle:[NSBundle mainBundle]]){
self.item = theItem;
self.title = [item objectForKey:@"link"];
}
return self;
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = [self.item objectForKey:@"link"];
NSURL *baseURL = [[NSURL URLWithString: urlAddress] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
[self.itemSummary loadHTMLString:urlAddress baseURL:nil];
[self.itemSummary loadRequest:request];
[baseURL release];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
现在,当我像这样分配地址时@"http://somesite.com"
,UIWebView 加载得很好,但是当我这样做时:NSString *urlAddress = [self.item objectForKey:@"link"];
它永远不会加载。正如我提到的,我检查了 的值[self.item objectForKey:@"link"];
是一个有效的 url,因为它显示在导航栏的标题中。
如果我这样做: [self.itemSummary loadHTMLString:urlAddress baseURL:nil]; UIWebView 显示 url,另一种验证 urlAddress 是否具有正确 url 的方法。
我究竟做错了什么?