【问题标题】:Converting a line of code to be compatible with storyboards in XCode 4.2将一行代码转换为与 XCode 4.2 中的故事板兼容
【发布时间】:2012-02-29 19:13:41
【问题描述】:

我有一个 rss 解析器,我正在将其转换为情节提要格式,但遇到了问题。当用户触摸具有 rss 提要的表格视图的一部分时,它会使用以下代码将视图推送到详细视图控制器:

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

return self;
}

当我运行它时,它运行良好,但当我尝试查看故事时崩溃。显然这是因为我使用了故事板,所以我不再有任何笔尖了,但是我该如何更改代码才能工作呢?

对不起,如果我的措辞不好。如果您有任何问题或需要澄清,我会在 cmets 中回答

【问题讨论】:

    标签: objective-c ios xcode xcode4.2 storyboard


    【解决方案1】:

    与其尝试使用详细视图控制器的自定义 init 方法设置属性值,在情节提要范式下处理此问题的更好方法是使用表视图控制器的 prepareForSegue: 方法。

    如果您在情节提要中设置从 tableview 控制器到详细视图控制器的 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"];
        }
    }
    

    【讨论】:

    • 非常感谢您的帮助。所以在我用这个替换我发布的代码之后,我的下一步是什么?
    • 应该是这样。要记住的几点: 1) 确保您已经在 tableview 控制器和细节控制器之间的故事板中创建了一个 segue(将属性 Inspector 中的 segue 的 Identifier 属性设置为“ShowDetail”)。 2) segue 现在将为您处理详细视图的实例化,因此您无需像在故事板到来之前那样在 didSelectRowAtIndexPath: 方法中执行任何操作。
    • NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; NSDictionary *theItem = [self.dataArray objectAtIndex:[indexPath row]];这两行给了我错误
    • 它说“'RSSDetailController * 类型的对象上找不到属性'tableView'”
    • 我给的方法prepareForSegue:属于你的tableView控制器类,而不是detail view控制器类。
    猜你喜欢
    • 2012-04-11
    • 2011-12-07
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    相关资源
    最近更新 更多