Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

viewDidLoad on DetailViewController after segue

Hi,

Can anyone help me with my viewDidLoad method on the DetailViewController.m after the prepareForSegue gets called.

This is the prepareForSegue

- (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.blogPost = blogPost;
    }

}

which passes this data

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];

I have a DetailViewController with this header file and 3 IBOutlets

@property (nonatomic, strong) BlogPost *blogPost;
@property (weak, nonatomic) IBOutlet UILabel *blogTitle;
@property (weak, nonatomic) IBOutlet UIImageView *blogImage;
@property (weak, nonatomic) IBOutlet UILabel *blogContent;

I think the app crashes because it doesn't know where to everything on the DetailViewController so could someone please help me with the implementation. Thanks

what error are you getting in the console? Also set an exception breakpoint for you project by going to debug -> breakpoints -> create exception breakpoint. This will cause your app to pause execution at the point where the exception is thrown. Cant really help you without more information about the error.

4 Answers

I added a breakpoint and the app pauses execution on the prepareForSegue method here

        BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];

I think the code there is good, please advise me if it isn't, but I think it stops running because i need a viewDidLoad on detailviewcontroller.m

you dont have a viewDidLoad?

to implement viewDidLoad in your detailViewController just use

- (void)viewDidLoad {
    [super viewDidLoad];
}

oh i have that, so i'm not sure what it is i'm missing then because the app fails when you i select a table view cell to go to the detail view

there should be an error in the console whenever it crashes. What is the error. copy and paste the error please.

without the breakpoint it crashes in the main

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

the debug console says

libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

when i have the breakpoint on it crashes on this line

        BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];

with (lldb) in the debug console and on the left hand side is

detailViewController    DetailViewController *  0x109251120 0x0000000109251120
UIViewController    UIViewController        
_blogPost   BlogPost *  nil 0x0000000000000000
_blogTitle  UILabel *   nil 0x0000000000000000
_blogImage  UIImageView *   nil 0x0000000000000000
_blogContent    UILabel *   nil 0x0000000000000000

I'm still new to the logic so does 'nil' mean it doesn't hold the blog post info, title and image etc or does it return nil because it hasn't found anywhere to put the info on the detailviewcontroller?