我正在开发一个新闻应用程序,从rss提要中获取内容。 当我点击TableViewCell时,我将带有标题,链接等的NSDictionary对象传递给下一个ViewController。在ViewController中我定义了NSDictionary * item;我可以通过设置viewcontroller的标题来验证值是否正确传递: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的方法。
我做错了什么?
答案 0 :(得分:0)
您在哪里为self.title
分配[item objectForKey:@"link"]
?
您是否在.h文件中添加了item
作为视图控制器的属性?如果您还没有使用上面发布的viewDidLoad
方法,则无法访问self.item
。
在你的.h文件中:
@interface MyViewController : UIViewController {
NSDictionary *item;
}
@property (nonatomic, readwrite, retain) NSDictionary *item;
在你的.m文件中(调整它以适合你的init方法):
-(id)initWithItem:(NSDictionary *)item {
if (self = [super initWithNibName: nil bundle: nil]) {
self.item = item;
}
return self;
}
执行此操作后,您将通过item
在视图控制器的方法中对self.item
属性进行全局访问。
答案 1 :(得分:0)
它已经解决了,我做了
NSURL *baseURL = [[NSURL URLWithString: [urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] retain];
现在有效,显然我在网址之后发送了一些空格,这就是为什么它不是一个有效的网址,即使它有效。我NSLog并得到了baseUrl http://site.com/item.php?id=2458%20%0A%09%20%20%20%20%20%20%20%20%09%20%20%20%20%09%09%09%09