RSSフィードからコンテンツを取得するニュースアプリを開発しています。TableViewCell をクリックすると、タイトル、リンクなどを含む NSDictionary オブジェクトを次の ViewController に渡します。ViewController で NSDictionary *item; を定義します。そして、viewcontroller のタイトルを次のように設定することで、値が正しく渡されていることを確認できます。タイトルは、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 を持っていることを確認する別の方法です。
私は何を間違っていますか?