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 trialalex santorineos
4,345 PointsI 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!!!
1 Answer
Kyle Pontius
6,190 PointsHey 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
:
- You need to make
line 21
optional so that you can assign it when you segue to thesecondViewController
(I cover that inStep 3
below). Changeline 21
insecondViewController.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!
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 theAttributes 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, useAddYourOwn
as the Storyboard Segue Identifier. Otherwise my code later on won't work.Now, go to the
ViewController.swift
. Here, before you segue to thesecondViewController.swift
, you need to assign thefactBook
variable in thesecondViewController
. You do this (in theViewController.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.
alex santorineos
4,345 Pointsalex santorineos
4,345 PointsThank 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!
Kyle Pontius
6,190 PointsKyle Pontius
6,190 PointsAlex,
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
alex santorineos
4,345 Pointsalex santorineos
4,345 Pointshere is the link for two question I posted. Thanks again!!!!
https://teamtreehouse.com/forum/on-the-second-view-controller-once-i-press-add-button-i-would-like-the-keyboard-to-automatically-go-down-and
https://teamtreehouse.com/forum/in-swift-to-go-back-from-the
alex santorineos
4,345 Pointsalex santorineos
4,345 Pointshttps://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!