4

我正在用UITableView構建一個iPhone應用程序。在選擇一行時,我想要一個UIWebView加載一個特定的URL。UITableView pushViewController選擇行

我目前有:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
NSDictionary * story = [stories objectAtIndex:[indexPath row]]; 
NSString * storyLink = [NSString stringWithFormat:[story objectForKey:@"link"]]; 

    [[self navigationController] pushViewController:[[FirstViewController alloc] initWithNibName:@"FirstView" bundle:[NSBundle mainBundle]] animated:YES]; 
} 

我怎樣才能像幻燈片中包含一個UIWebView和隨後加載故事鏈接到它通過新的觀點:

loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]] 

在此先感謝,

Benji

回答

9

正如其他人所說,一般的想法是有存儲需要加載的URL FirstViewController的屬性,然後將該URL加載到一個UIWebView的看法出現時。

下面是它可能是什麼樣子,用頭開始一個例子:

@interface FirstViewController : UIViewController { 
    UIWebView *webView; 
    NSURL *storyURL; 
} 
@property (nonatomic, retain) IBOutlet UIWebView *webView; // this is set up in your nib 
@property (nonatomic, retain) NSURL *storyURL; 
@end 

現在的落實:

@implementation FirstViewController 

@synthesize webView; 
@synthesize storyURL; 

- (void)dealloc; 
{ 
    [storyURL release]; // don't forget to release this 
    [super dealloc]; 
} 

- (void)viewDidAppear:(BOOL)animated; 
{ 
    // when the view appears, create a URL request from the storyURL 
    // and load it into the web view 
    NSURLRequest *request = [NSURLRequest requestWithURL:self.storyURL]; 
    [self.webView loadRequest:request]; 
} 

- (void)viewWillDisappear:(BOOL)animated; 
{ 
    // there is no point in continuing with the request load 
    // if somebody leaves this screen before its finished 
    [self.webView stopLoading]; 
} 
@end 

所以,現在你只需要在控制器的做先前的視圖獲取故事URL,將其傳遞給FirstViewController並將其推送。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSDictionary *story = [stories objectAtIndex:[indexPath row]]; 
    NSURL *storyURL = [NSURL URLWithString:[story objectForKey:@"link"]]; 
    FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:[NSBundle mainBundle]]; // I'm pretty sure you don't need to pass in the main bundle here 
    firstViewController.storyURL = storyURL; 
    [self.navigationController pushViewController:firstViewController animated:YES]; 
    [firstViewController release]; // don't leak memory 
} 

就是這樣。其他一些要點:

  • 我承擔你的字典「鏈接」值已經是一個字符串 - 在你原來的例子一個全新的字符串的建設是不必要的,如果是這種情況。正如你在我的例子中看到的,我們可以直接使用這個字符串來創建NSURL實例。

  • 在你原來的代碼中,當你分配/ init你的FirstViewController時,你直接把它傳遞給pushViewController。這會造成內存泄漏,因爲一旦UINavigationController完成(在它從導航堆棧彈出之後),它的保留計數仍然是1.至少應該調用autorelease,但這裏要做的最有效的事情就是alloc /初始化它,將它存儲在一個臨時變量中,然後在我們推送它之後立即調用release。這確保了一旦它從導航堆棧彈出,它就會被釋放。

+0

真棒解釋 - 非常感謝! – 2009-07-24 20:06:00

+0

我會在'viewDidLoad'而不是'viewDidAppear'中執行'loadRequest',否則,你會繼續重新加載頁面。你有一個可以切換視圖的標籤控制器。 – 2009-07-24 21:41:36

+0

對於學習,我可以請求一個可下載的示例應用程序在XCode中查看以上內容嗎? – testndtv 2011-10-30 09:47:48

0

要獲得uiwebview只需創建它與I nterface Builder或將其定義爲正在加載的視圖控制器中的實例變量。之後,使用nsurlrequest將工作

0

最好的辦法是創建一個新的視圖控制器類,其中包含一個web視圖。該視圖控制器將具有url屬性,該屬性在viewDidLoad中時在Web視圖上傳遞。

+0

如何添加一個url屬性到視圖控制器? – 2009-07-24 15:57:23

+0

閱讀[Objective-C 2.0編程語言](http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html)。 – 2009-07-24 16:23:56

1

首先,您的項目中是否有名爲「FirstView」的筆尖?

其次,我通常這樣做的方式是實例化ViewController並在將參數傳遞到導航堆棧之前將參數傳遞給它。

UIViewController *myViewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:[NSBundle mainBundle]]; 
myViewController.story = storyLink; 
[[self navigationController] pushViewController:myViewController animated:YES]; 
+0

是的,我有一個FirstView筆尖。你的例子是拋出我和錯誤 - 請求成員'故事'的東西不是結構或聯盟。我想它因此不喜歡\t myViewController.story = storyLink; – 2009-07-24 15:46:55

0

爲此,您可以在FirstViewController一個IBOutlet中的WebView然後通過使FirstViewController

的對象發送URL鏈接

object.urllink = storyUrl;

那麼你可以在UIWebView中打開該鏈接.....