Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Whats wrong with my code?

Im trying to use Segue, to tell me the capital on a Second View Controller. There are no errors in Xcode, but the label on Second Viewcontroller doesn't show me anything? You can see the codes below

import UIKit

class ViewController: UIViewController {

    var lande = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }

    @IBAction func spainKnap(sender: UIButton) {
        lande = lande + 1
    }

    @IBAction func denmarkKnap(sender: UIButton) {
        lande = lande + 2
    }

    @IBAction func italyKnap(sender: UIButton) {
        lande = lande + 3
    }


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

            let sideEtKontrol = segue.destinationViewController as! SecondVC


            if lande == 1 {
                sideEtKontrol.segueText = "Madrid"
            }
            else if lande == 2 {
                sideEtKontrol.segueText = "København"
            }
            else if lande == 3 {
                sideEtKontrol.segueText = "Rom"
            }

            else {
                sideEtKontrol.segueText = "Du har ikke trykket"
            }

        }

    }
}
import Foundation
import UIKit

class SecondVC: UIViewController {


    @IBOutlet var labelText: UILabel!

    var segueText: String = ""

    override func viewDidLoad() {

        super.viewDidLoad()

        labelText.text = segueText

    }

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


}

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hey Jonas: Try this

import UIKit

class ViewController: UIViewController {

    var lande = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }

    @IBAction func spainKnap(sender: UIButton) {
        lande = lande + 1
        performSegueWithIdentifier("sideSkift", sender: sender)

    }

    @IBAction func denmarkKnap(sender: UIButton) {
        lande = lande + 2
        performSegueWithIdentifier("sideSkift", sender: sender)
    }

    @IBAction func italyKnap(sender: UIButton) {
        lande = lande + 3
        performSegueWithIdentifier("sideSkift", sender: sender)
    }


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

            let sideEtKontrol = segue.destinationViewController as! SecondVC


            if lande == 1 {
                sideEtKontrol.segueText = "Madrid"
            }
            else if lande == 2 {
                sideEtKontrol.segueText = "København"
            }
            else if lande == 3 {
                sideEtKontrol.segueText = "Rom"
            }

            else {
                sideEtKontrol.segueText = "Du har ikke trykket"
            }

        }

    }
}

Second VC

import Foundation
import UIKit

class SecondVC: UIViewController {


    @IBOutlet var labelText: UILabel!

    var segueText = ""

    override func viewDidLoad() {

        super.viewDidLoad()

        labelText.text = segueText

    }

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


}

It works! Thanks a lot, i really wondered how you can pass difference data with difference buttons! But now i got a new problem. How do i get my variable called "lande" to be 0 everytime i go back to the First View? Now it puts the value of the button together with the last button i pressed. Can i make somekind of Default in the segue function?