【发布时间】:2025-05-08 06:00:01
【问题描述】:
我正在开发一个从 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 的方法。
我做错了什么?
【问题讨论】:
-
显示创建新视图控制器的代码并将项目传递给它。完全按照您的方式显示所有代码。您可以在此处简单地编辑您的问题并将该代码添加到它的末尾。
-
在导航栏中显示为标题不一定是有效 URL 的证据。如果你 NSLog 它(或设置断点并 po 它),它是什么样的?
-
我 NSLog 发现 baseURL 返回 nil,但 urlAddress 有一个“有效”的 url,所以当我执行 NSURL *baseURL = [[NSURL URLWithString: urlAddress] 保留]; baseURL 没有正确初始化,也许它与编码有关?
标签: iphone ios4 uiwebview nsurl nsurlrequest