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

I am making an app on swift. It is very similar to the first app project that was on the Swift Track...

the only difference is I have a second view controller. I append a new string to the array. But once I go back to main view controller whatever I added is gone. Please help!!!

https://www.dropbox.com/s/wmg4jc7ybwie93u/guessWho.zip?dl=0

1 Answer

Hey Alex,

You have a great question. I reviewed your code and the problem stems from something that's fairly easy thing to miss. In your code on Dropbox:

Your secondViewController.swift class is instantiated when you segue to it from the ViewController.swift view. When the secondViewController is instantiated, line 21 in secondViewController tells the program to create a completely new Factbook(), not borrowing the same one that came from the original ViewController view. To solve this you need to take 3 steps to pass that factBook forward from ViewController:

  1. You need to make line 21 optional so that you can assign it when you segue to the secondViewController (I cover that in Step 3 below). Change line 21 in secondViewController.swift to be (I'd copy it):
var factBook:FactBook?

Now that you changed line 21 to optional, you need to update some code in secondViewController.swift to handle its optionalness. In the addNewString() function, update it to be (I'd also copy the whole thing):

@IBAction func addNewString() {
    var name = myText.text
    factBook!.factsArray.append(name)

    println(factBook!.factsArray)
}

Still in secondViewController.swift, add the following function so you can set the factBook variable easily:

func setFactBook(newFactBook:FactBook) {
        factBook = newFactBook
}

Now secondViewController.swift should be ready to go!

  1. This step is fairly easy: A. Go to your Main.storyboard and click on the segue icon (that arrow with a circle BETWEEN the two view controllers). B. After clicking on the segue icon, go to the Attributes Inspector. --> See this post if you're not sure where that is at: http://stackoverflow.com/questions/10471975/show-view-attributes-inspector-in-xcode C. In the ID field, enter an identifier. If you want my code that I'm going to give you next, use AddYourOwn as the Storyboard Segue Identifier. Otherwise my code later on won't work.

  2. Now, go to the ViewController.swift. Here, before you segue to the secondViewController.swift, you need to assign the factBook variable in the secondViewController. You do this (in the ViewController.swift) by adding the following method:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "AddYourOwn" {
        var secondController:secondViewController = segue.destinationViewController as secondViewController
        secondController.setFactBook(self.factBook)
    }
}

Notice: The if segue.identifier is checking if the segue is AddYourOwn, like we talked about before. In general, this function checks to see if you're seguing to the secondViewController. You can use it to prepare the whole view. The line that starts var secondController... is grabbing the secondViewController you're about to segue into. Then the next line uses that setFactBook method on the newly created secondViewController, to set that new factBook to the same factBook you're using in the ViewController.

This way, you're passing the same factBook around, instead of creating a new one.

Good luck, let me know if I missed something or you have any questions!

-- Kyle

PS -- If this is helpful, please mark it as the best answer for future users.

Thank you so much!!!!! That worked.

To go back from the secondViewController to the ViewController I would like to swipe from left to right, on the screen. How do I do that?

When I go to mainStoryboard it does not allow me to access constraints. Please help me!

Alex,

I'm glad to hear that it worked well for you!

I would suggest you post your new question as a separate post on the forum. That way people will see it and have a chance to answer it. I'll watch for your new post, and answer it if I have a few minutes in the next day or two.

Good luck -- Kyle

https://teamtreehouse.com/forum/core-location-framework-swift

can you take a look at the question I've posted and try to help me? Thank you very much!