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 Swift Using a Navigation Controller With Segues Modifying the UI

Eunice Cheung
Eunice Cheung
1,987 Points

Info is not being passed between views

Hi, I’ve been working on Algorythm, on Modifying the UI, using Xcode 6.3, and I have run into the same problem as some of the other students, namely the program wants to use as! instead of as.

However, when I press the button on the main view controller, activating the “prepare for segue” method, it won’t change the variable in the other view controller - the view controller stays at “you have not pressed the button yet” and won’t change.

How can I pass information between views? What am I doing wrong?

Chris Shaw
Chris Shaw
26,676 Points

Hi Eunice,

Could you please post the code you currently have for you segue and view controller you're attempting to pass data to.

https://teamtreehouse.com/forum/posting-code-to-the-forum

Eunice Cheung
Eunice Cheung
1,987 Points

The Detail View Controller:

import UIKit
class PlaylistDetailViewController: UIViewController {
    @IBOutlet weak var pressedLabel: UILabel!
    var seguelabel: String?
    override func viewDidLoad() {
        super.viewDidLoad()
        if seguelabel != nil {
            pressedLabel.text=seguelabel
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

And the Master View Controller:

import UIKit
class PlaylistMasterViewController: UIViewController {
    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var playlistImage0: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        button.setTitle("Happy Mothers Day!", forState: .Normal)
        let playlist=Playlist(index: 0)
        playlistImage0.image=playlist.icon
        playlistImage0.backgroundColor = playlist.backgroundColor
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showPlaylistDetail" {
            let playlistDetailController = segue.destinationViewController as!  PlaylistDetailViewController
            playlistDetailController.seguelabel = "You have pressed the button"
        }
    }
}

1 Answer

Eunice Cheung
Eunice Cheung
1,987 Points

I entered the wrong segue name.