2012-02-29 35 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