[英]Converting a line of code to be compatible with storyboards in XCode 4.2
我有一個RSS解析器,正在將其轉換為情節提要格式,但遇到了問題。 當用戶觸摸具有rss feed的表視圖部分時,它將使用以下代碼將視圖推入詳細視圖控制器:
- (id)initWithItem:(NSDictionary *)theItem {
if (self == [super initWithNibName:@"RssDetailController" bundle:nil]) {
self.item = theItem;
self.title = [item objectForKey:@"title"];
}
return self;
}
當我運行它時,它工作正常,但是當我嘗試查看該故事時崩潰。 顯然這是因為使用情節提要板后我不再有任何筆尖,但是我將如何更改代碼使其工作?
對不起,如果我的措辭不好。 如果您有任何疑問或需要澄清,我會在評論中回答
代替嘗試使用詳細視圖控制器的自定義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"];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.