0

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 を持っていることを確認する別の方法です。

私は何を間違っていますか?

4

2 に答える 2

0

解決しました、やった

NSURL *baseURL = [[NSURL URLWithString: [urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] retain]; 

どうやらURLの後にいくつかのスペースを送信していたようです。そのため、有効に見えても有効なURLではありませんでした。私は 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

于 2010-12-30T04:01:56.200 に答える
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;
}

これを行うと、View Controller のメソッドで をitem介してプロパティにグローバル アクセスできるようになりますself.item

于 2010-12-29T21:32:25.150 に答える