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 Playlist Browser with Objective-C Building the Master and Detail Views Connecting UI to Code

background color is not updating properly for the first image nor is it updating to the UIImageView, code is identical.

All the code looks the same from start to finish, everything else works but for some reason the backgroundColor isn't updating properly from the colorDictionary

//
//  Playlist.m
//  teamtreehouse_multiviews_ex1
//
//  Created by Robert Mcelvenny on 3/11/15.
//  Copyright (c) 2015 Robert Mcelvenny. All rights reserved.
//

#import "Playlist.h"
#import "MusicLibrary.h"

@implementation Playlist


-(instancetype)initWithIndex:(NSUInteger)index {
    self = [super init];
    if(self) {
        MusicLibrary *musicLibrary = [[MusicLibrary alloc] init];
        NSArray *library = musicLibrary.library;

        NSDictionary *playlistDictionary = library[index];

        _playlistTitle = [playlistDictionary objectForKey:kTitle];
        _playlistDescription = [playlistDictionary objectForKey:kDescription];

        NSString *iconName = [playlistDictionary objectForKey:kIcon];
        _playlistIcon = [UIImage imageNamed:iconName];

        NSString *largeIconName = [playlistDictionary objectForKey:kLargeIcon];
        _playlistIconLarge = [UIImage imageNamed:largeIconName];

        _playlistArtists = [NSArray arrayWithArray:[playlistDictionary objectForKey:kArtists]];

        NSDictionary *colorDictionary = [playlistDictionary objectForKey:kBackgroundColor];
        _backgroundColor = [self rgbColorFromDictionary:colorDictionary];

    }
    return self;
}

-(UIColor *)rgbColorFromDictionary:(NSDictionary *)colorDictionary {
    CGFloat red = [[colorDictionary objectForKey:@"red"] doubleValue];
    CGFloat green = [[colorDictionary objectForKey:@"green"] doubleValue];
    CGFloat blue = [[colorDictionary objectForKey:@"blue"] doubleValue];
    CGFloat alpha = [[colorDictionary objectForKey:@"aplha"] doubleValue];

    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha];
            }
@end

@end
//
//  Playlist.h
//  teamtreehouse_multiviews_ex1
//


#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface Playlist : NSObject

@property (strong, nonatomic) NSString *playlistTitle;
@property (strong, nonatomic) NSString *playlistDescription;
@property (strong, nonatomic) UIImage *playlistIcon;
@property (strong, nonatomic) UIImage *playlistIconLarge;
@property (strong, nonatomic) NSArray *playlistArtists;
@property (strong, nonatomic) UIColor *backgroundColor;

-(instancetype) initWithIndex:(NSUInteger)index;
@end
#import "PlaylistDetailViewController.h"
#import "Playlist.h"

@interface PlaylistDetailViewController ()

@end

@implementation PlaylistDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.playlist) {
        self.playlistCoverImage.image = self.playlist.playlistIconLarge;
        self.playlistCoverImage.backgroundColor = self.playlist.backgroundColor;
        self.playlistTitle.text = self.playlist.playlistTitle;
        self.playlistDescription.text = self.playlist.playlistDescription;

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
#import "MusicLibrary.h"

// create a constant pointer to a string.
NSString *const kTitle = @"title";
NSString *const kDescription = @"description";
NSString *const kIcon = @"icon";
NSString *const kLargeIcon = @"largeIcon";
NSString *const kBackgroundColor = @"backgroundColor";
NSString *const kArtists = @"artists";

@implementation MusicLibrary
@synthesize library;

- (instancetype)init
{
    self = [super init];
    if (self) {
        library = @[@{kTitle: @"Rise and Shine",
                      kDescription: @"Get your morning going by singing along to these classic tracks as you hit the shower bright and early!",
                      kIcon: @"coffee.pdf",
                      kLargeIcon: @"coffee_large.pdf",
                      kBackgroundColor: @{@"red": @255.0, @"green": @204.0, @"blue": @36.0, @"alpha": @1.0},
                      kArtists: @[@"American Authors", @"Vacationer", @"Matt and Kim", @"MGMT", @"Echosmith", @"Tokyo Police Club", @"La Femme"]
                      },
                    @{kTitle: @"Runner's Rampage",
                      kDescription: @"Hit the track hard and get in beast mode with everything from dance tracks to classic hip hop. All the right fuel to motivate you to push your limits.",
                      kIcon: @"running.pdf",
                      kLargeIcon: @"running_large.pdf",
                      kBackgroundColor: @{@"red": @255.0, @"green": @102.0, @"blue": @51.0, @"alpha": @1.0},
                      kArtists: @[@"Avicii", @"Calvin Harris", @"Pitbull", @"Iggy Azalea", @"Rita Ora", @"Drake", @"Lil Wayne"]
                      },
                    @{kTitle: @"Joy Ride",
                      kDescription: @"Let this eclectic playlist take you wherever your heart desires. Cruise along in style to these energetic beats.",
                      kIcon: @"helmet.pdf",
                      kLargeIcon: @"helmet_large.pdf",
                      kBackgroundColor: @{@"red": @153.0, @"green": @102.0, @"blue": @204.0, @"alpha": @1.0},
                      kArtists: @[@"Afrojack", @"Kid Cudi", @"Daft Punk", @"kIcona Pop", @"Gesaffelstien", @"Roksnoix", @"deadmau5"]
                      },
                    @{kTitle: @"In The Zone",
                      kDescription: @"Keep calm and focus. Shut out the noise around you and grind away with some mind sharpening instrumental tunes.",
                      kIcon: @"laptop.pdf",
                      kLargeIcon: @"laptop_large.pdf",
                      kBackgroundColor: @{@"red": @51.0, @"green": @153.0, @"blue": @204.0, @"alpha": @1.0},
                      kArtists: @[@"Dr. Dre", @"Snoop Dogg", @"Com Truise", @"D12", @"Flying Lotus", @"Kanye West", @"Rundfunk"]
                      },
                    @{kTitle: @"Button Masher",
                      kDescription: @"Turn up the speakers and get out of my way! The ultimate gaming playlist to get you hyped up and ready for the crazy fun that’s about to happen.",
                      kIcon: @"joystick.pdf",
                      kLargeIcon: @"joystick_large.pdf",
                      kBackgroundColor: @{@"red": @51.0, @"green": @204.0, @"blue": @102.0, @"alpha": @1.0},
                      kArtists: @[@"Skrillex", @"The Game", @"2 Chainz", @"Jay Z", @"T.I.", @"Rihanna", @"Eminem"]
                      },
                    @{kTitle: @"Futbal Remix",
                      kDescription: @"There’s nothing like the world’s game. Kick around the field with this eclectic playlist from around the world. Futbal for life.",
                      kIcon: @"ball.pdf",
                      kLargeIcon: @"ball_large.pdf",
                      kBackgroundColor: @{@"red": @255.0, @"green": @102.0, @"blue": @153.0, @"alpha": @1.0},
                      kArtists: @[@"Shakira", @"Santana", @"Wyclef Jean", @"Aloe Blacc", @"Pitbull", @"Enrique Iglesias", @"Ricky Martin"]

                      }];
    }

return self;
}

@end

Look in rgbColorFromDictionary method. You have misspelled alpha.

2 Answers

ILook in rgbColorFromDictionary method. You have misspelled alpha.

I can't believe just that slight typo broke it. :P Ty again!

I can't believe just that slight typo broke it. :P Ty again!

Damien Watson
Damien Watson
27,419 Points

Hey Robert, A bit hard to tell without being able to debug myself, but a quick thing you might be able to check:

  • PlaylistDetailViewController -> Instead of setting background color to 'self.playlist.backgroundColor' try setting it to [UIColor redColor]. This will identify if the background is being set properly.

  • If that works, check the color is successfully being created and passed in.

Hope that helps some.

Damien Watson
Damien Watson
27,419 Points

Ooops, I didn't mean to put this in as an answer... I think Deanne is on the money.

Thank you though!