objective-c - 将一行代码转换为与 XCode 4.2 中的 Storyboard兼容

标签 objective-c ios xcode xcode4.2 storyboard

我有一个 rss 解析器,正在将其转换为 Storyboard格式,但遇到了问题。当用户触摸 TableView 中包含 rss feed 的部分时,它会使用以下代码将该 View 推送到详细 View Controller :

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

return self;
}

当我运行它时,它工作正常,但当我尝试查看故事时它崩溃了。显然,这是因为由于使用 Storyboard,我不再有任何 Nib ,但我如何更改代码才能工作?

抱歉,如果我的措辞不好。如果您有任何疑问或需要澄清,我会在评论中回答

最佳答案

不要尝试使用详细 View Controller 的自定义 init 方法来设置属性值,在 Storyboard范例下处理此问题的更好方法是使用以下方法的 prepareForSegue: 方法:你的表格 View Controller 。

如果您在 Storyboard中设置了从表格 View Controller 到详细 View Controller 的 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"];
    }
}

关于objective-c - 将一行代码转换为与 XCode 4.2 中的 Storyboard兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9505186/

相关文章:

ios - Autolayout 和 Scrollview 宽度固定到屏幕(不是内容)

ios - 覆盖-tableView :commitEditingStyle:forRowAtIndexPath: to display info

objective-c - 从另一个 Controller 和 Nib 加载 View

iphone - 使用后退按钮时 UISwitch 会切换到 OFF 状态

ios - 如何启用自定义 CBDescriptor 值?

ios - 日志记录和断点在 XCTests 中不起作用

ios - 警告 : 'isTouchInside' in If Condition

objective-c - 以编程方式 LZMA 解压缩静态 LZMA 压缩文件

ios - UIScrollViewKeyboardDismissModeOnDrag 当没有滚动不起作用时

iphone - 供应商标识符