2014-02-21 63 views
0

我遇到了prepareForSegue方法推送DetailViewController的問題。使用prepareforSegue到DetailViewController

我一直在關注很多教程,但是當我想使用我自己的數據時,我顯然沒有做出正確的選擇。

我的tableView加載好了這段代碼,然後我想在用戶選擇博客文章時傳遞給detailviewcontroller。

NSURL *blogURL = [NSURL URLWithString:@"http://theforeground.co.uk/feed/json"]; 

NSData *jsonData = [NSData dataWithContentsOfURL:blogURL]; 

NSError *error = nil; 

self.blogPosts = [NSMutableArray array]; 
NSArray *blogPostsArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; 


for (NSDictionary *bpdictionary in blogPostsArray) { 
    BlogPost * blogPost = [BlogPost blogPostWithTitle:[bpdictionary 
     objectForKey:@"title"]]; 
    blogPost.excerpt = [bpdictionary objectForKey:@"excerpt"]; 
    blogPost.thumbnail = [bpdictionary objectForKey:@"thumbnail"]; 
    blogPost.date = [bpdictionary objectForKey:@"date"]; 
    blogPost.content = [bpdictionary objectForKey:@"content"]; 
    [self.blogPosts addObject:blogPost]; 
} 

我從tableviewcell到detailviewcontroller一個SEGUE並稱之爲「ShowDetailView」,這是prepareForeSegue方法我用

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"ShowDetailView"]) 
    { 
     DetailViewController *detailViewController = [segue destinationViewController]; 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row]; 

    } 

我已經進口「detailviewcontroller.h」「 tableviewcontroller.m「,我有detailviewcontroller頭文件這些propertys

​​

請有人可以幫助這個初學者。由於

回答

1

您檢索數據源中的數據,但你不將它傳遞給視圖控制器,請嘗試:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"ShowDetailView"]) 
    { 
     DetailViewController *detailViewController = [segue destinationViewController]; 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row]; 

     // You passing the data here to detail view controller 
     detailViewController.blogPost = blogPost; 

    } 
} 

您還需要添加博客帖子財產detailviewcontroller.h文件:

@property (nonatomic, strong) BlogPost *blogPost; 

之後,您可以使用它,例如在detailviewcontroller.m文件中的viewDidLoad方法。

+0

謝謝你,我認爲這糾正了prepareForSegue,但它會繼續崩潰,直到我得到detailviewcontroller.m文件的viewDidLoad權利。你能幫助它看起來應該如何?我有IBOutlets的博客標題,圖像和內容開始。謝謝 –

+0

發佈崩潰日誌和viewDidLoad方法。 – Greg

+0

由於格雷格,這裏發生了錯誤'博客帖子*博文= [self.blogPosts objectAtIndex:indexPath.row];' 和viewDidLoad方法是' - (無效)viewDidLoad中 { [超級viewDidLoad中];' –

相關問題