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 Build a Diary App Using Core Data Custom Detail View Controller Entry Detail View Controller

diary app mood change

how can i modify my code so that users can pick another mood because as it is now the good mood is default and does not change even if i take on any of the other moods when putting in an entry. Ash Furrow

Ash Furrow
Ash Furrow
Treehouse Guest Teacher

Hi there! I think I addressed that in the video right after that (it's been a few months since I recorded it). Check it out and let me know if that doesn't work.

Yeah it should work.. I remember doing this app, it was awesome! Kelvin if you get stuck let me know. Post the code, and I'll help you.

3 Answers

Chris Jones
STAFF
Chris Jones
Treehouse Guest Teacher

I believe I finally found the problem. To quickly reproduce the error, I went to the final video in this series and downloaded Diary-Stage6-Video5.zip. Run the app and add some entries. No matter what mood you select, the main list view will always display a good mood.

The problem lies in the THEntryViewController.m file. For me, this is line 110 within the insertDiaryEntry method.

- (void)insertDiaryEntry {
    THCoreDataStack *coreDataStack = [THCoreDataStack defaultStack];
    THDiaryEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"THDiaryEntry" inManagedObjectContext:coreDataStack.managedObjectContext];
    entry.body = self.textView.text;
    entry.date = [[NSDate date] timeIntervalSince1970];
    entry.imageData = UIImageJPEGRepresentation(self.pickedImage, 0.75);
    entry.location = self.location;
    self.entry.mood = self.pickedMood; // <----- THIS LINE
    [coreDataStack saveContext];
}

The entry isn't being saved properly. This should be:

entry.mood = self.pickedMood;

It's possible I missed something in the series. However, I don't ever remember this being addressed and I had this bug in my final version of the app and the final project files I downloaded just a few minutes ago.

Roman Davydko
Roman Davydko
2,198 Points

I had the same problem and answers above didn't help me. So I solved it myself! (:<br /> The problem was in "updateDiaryEntry" method. Ash didn't cover this in the video (that's why all his entries have "Good" mood).<br /> So everything you need to do is to make this method to save mood, not just body text:<br />

- (void) updateDiaryEntry {
    self.entry.body = self.textField.text;
    self.entry.mood = self.pickedMood; // <-- This line!

    JDCoreDaraStack *coreDataStack = [JDCoreDaraStack defaultStack];
    [coreDataStack saveContext];
}

I hope it will help you as well.

Here is the code:

-(void)configureCellForEntry:(MavDiaryEntry *)entry {
    self.bodyLabel.text = entry.body;
    self.locationLabel.text = entry.location;

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEEE, MMMM d yyyy"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:entry.date];

    self.dateLabel.text = [dateFormatter stringFromDate:date];

    if (entry.imageData) {
        self.mainImageView.image = [UIImage imageWithData:entry.imageData];
    }else {
        self.mainImageView.image = [UIImage imageNamed:@"icn_noimage"];
    }

//MOOD SETTINGS:
    if (entry.mood == MavDiaryEntryMoodGood) {
        self.moodImageView.image = [UIImage imageNamed:@"icn_happy"];
    } else if (entry.mood == MavDiaryEntryMoodAverage) {
        self.moodImageView.image = [UIImage imageNamed:@"icn_average"];
    } else if (entry.mood == MavDiaryEntryMoodBad) {
        self.moodImageView.image = [UIImage imageNamed:@"icn_bad"];
    }
    self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame) / 2.0f;

    if (entry.location.length > 0) {
        self.locationLabel.text = entry.location;
    }else {
        self.locationLabel.text = @"Somewhere on Earth";
    }
}

It's working for me just fine : )

Chris Jones
Chris Jones
Treehouse Guest Teacher

I just completed the last video thinking this functionality would be implemented and it wasn't. To double check my code I downloaded the final project files from the last video and ran the app. Moods do not change no matter what selection you make. If it was covered, that code didn't make it into the final project files.

Did you try the code above?