我有一个 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/