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

Making a default value when i go back

How can i make a default value everytime i go back to my FirstViewController? Ive got two viewcontrollers. The FirstViewController has four buttons, with four different values, so the Labeltext on my SecondViewController will be different for each button. But when i go back from the SecondView to the FirstView and press a new button, it will put the values together, and makes a wrong answer. I will show you the codes below, and i really hope you guys can help!

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 spainBut(sender: UIButton) {
        lande = lande + 1
        performSegueWithIdentifier("sideSkift", sender: sender)
    }

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

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




    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "sideSkift" {
            let sideKontrol = segue.destinationViewController as! SecondVC

            if lande == 1 {
                sideKontrol.segueText = "Madrid"
            }
            else if lande == 2 {
                sideKontrol.segueText = "København"
            }
            else if lande == 3 {
                sideKontrol.segueText = "Rom"
            }
            else {
                sideKontrol.segueText = "Du har ikke trykket"
            }

        }

    }




}
import Foundation
import UIKit



class SecondVC: UIViewController {

    @IBOutlet var buttonLabel: UILabel!
    var segueText: String = ""


    override func viewDidLoad() {
        super.viewDidLoad()

        buttonLabel.text = segueText
    }

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


}

2 Answers

You will have to clear the array to do what you want I think, as in re-assigning the array after you go back to the first view-controller.

Can you make me an example?

How do i re-assign the array after i go back to FirstView?

It might be something like this for the button:

@IBAction func homeBut(sender: UIButton) {
        lande = 0
       performSegueWithIdentifier("sideSkift", sender: sender)
 }

I'm not sure you would need an if statement.

I see the idea. But i can't add my "Go back" button from my SecondView to Firstview when im using the navigation?