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

Help me please!

lesson: https://teamtreehouse.com/library/build-a-playlist-browser-with-swift/refactoring-our-code/passing-playlist-objects-using-segues

Problem : Not working

P.S instead of PlaylistDetailController -> ViewControllerTwo

import UIKit

class ViewController: UIViewController {

    var playlistsArray: [UIImageView] = []

    @IBOutlet weak var playlistImageView0: UIImageView!

    override func viewDidLoad() {
            super.viewDidLoad()

        for index in 0..<playlistsArray.count {
        let playlist = PlayList(index: index)
        let playlistImageView = playlistsArray[index]

        playlistImageView.image = playlist.icon
        playlistImageView.backgroundColor = playlist.backgroundColor
        }

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showPlayListDetailSegue"{

            let playlistImageView = sender!.view as! UIImageView
            if let index = playlistsArray.indexOf(playlistImageView){
                let viewControllerTwo = segue.destinationViewController as! ViewControllerTwo
                viewControllerTwo.playList = PlayList(index:index)

            }
        }
    }


    @IBAction func showPlaylistDetail(sender: AnyObject) {
        performSegueWithIdentifier("showPlayListDetailSegue", sender: sender)
    }


}

1 Answer

Andrey,

Try this:

import UIKit

class PlaylistMasterViewController: UIViewController {

    var playlistsArray: [UIImageView] = []
    @IBOutlet weak var playlistImageView0: UIImageView!


    override func viewDidLoad() {
        super.viewDidLoad()

        playlistsArray.append(playlistImageView0)

        for index in 0..<playlistsArray.count {
            let playlist = Playlist(index: index)
            let playlistImageView = playlistsArray[index]

            playlistImageView.image = playlist.icon
            playlistImageView.backgroundColor = playlist.backgroundColor
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showPlaylistDetailSegue" {

            let playlistImageView = sender!.view as! UIImageView
            if let index = playlistsArray.indexOf(playlistImageView) { //was find(playlistsArray, playlistImageView) {
                let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
                playlistDetailController.playlist = Playlist(index: index)
            }


        }
    }

    @IBAction func showPlaylistDetail(sender: AnyObject) {
        performSegueWithIdentifier("showPlaylistDetailSegue", sender: sender)
    }

}

If this doesn't work, can you post what error you are getting?

Florin

Thank you!