2012-02-29 27 views
0

我有一個RSS解析器,我轉換成故事板的格式,我遇到了一個問題。當用戶觸摸具有RSS提要的表視圖的一部分,它推動該視圖的詳細視圖控制器使用此代碼:轉換一行代碼是兼容的故事板中的XCode 4.2

- (id)initWithItem:(NSDictionary *)theItem { 
if (self == [super initWithNibName:@"RssDetailController" bundle:nil]) { 
    self.item = theItem; 
    self.title = [item objectForKey:@"title"]; 
} 

return self; 
} 

當我運行它,它工作正常,但死機當我嘗試看故事。顯然這是因爲我沒有任何筆尖,因爲使用了故事板,但是我將如何更改代碼來工作?

很抱歉,如果我的措辭是壞的。如果您有任何疑問或需要澄清,我會在評論中回答

回答

1

而不是嘗試使用您的詳細視圖控制器的自定義init方法設置屬性值,在故事板範例下處理此問題的更好方法是應該使用表格視圖控制器的prepareForSegue:方法。

如果從實現代碼如下控制器到您的詳細視圖控制器設置在故事情節進行SEGUE,這個方法會被當SEGUE發生調用。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{   
    if ([segue.identifier isEqualToString:@"ShowDetail"]) { // be sure to name your segue in storyboard 

     // sender in this case is the tableview cell that was selected 
     UITableViewCell *cell = sender; 

     // get the index path for the selected cell 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

     // use the indexPath to get the appropriate item from your data source 
     NSDictionary *theItem = [self.dataArray objectAtIndex:[indexPath row]]; // or whatever 

     // get the view controller you are about to segue to 
     RssDetailController *rssDetailvc = [segue destinationViewController]; 

     // set the properties 
     rssDetailvc.item = theItem; 
     rssDetailvc.title = [theItem objectForKey:@"title"]; 
    } 
} 
+0

非常感謝您的幫助。所以在我替換了我發佈的代碼之後,我的下一步是什麼? – Sam 2012-02-29 19:53:48

+0

應該是這樣。要記住的要點:1)確保你在tableview控制器和詳細控制器之間的故事板中創建了一個segue(在屬性Inspector中將segue的Identifier屬性設置爲「ShowDetail」)。 2)segue現在會爲你處理你的詳細視圖的實例化,所以你不需要在你的'didSelectRowAtIndexPath:'方法中做任何事情,就像在故事板到來之前你必須做的那樣。 – jonkroll 2012-02-29 19:59:54

+0

NSIndexPath * indexPath = [self.tableView indexPathForCell:cell]; NSDictionary * theItem = [self.dataArray objectAtIndex:[indexPath row]];這兩條線給我的錯誤 – Sam 2012-02-29 20:13:16