-1

So I want to be able to pass a variable from one view to another view easily. I've seen ways by doing it via a Segue but I was hoping for something simpler than that if possible.

Is there any way to just transfer the content of one variable and save it in another view really easily. I am aware this might not be possible.

If this isn't possible, please could someone explain how to use the Segue method? I've seen various online tutorials explaining how to do it but they are all applying it to a situation. I just want to know the bare code and how it works then apply it to my own situation. So just basically a way to transfer a variable from one view to another that someone with very very little knowledge like me will understand. I understand terminology as I program in other languages but swift is very new to me.

I should add that the coding language I am using is swift.

I would appreciate it if someone could help me with this.

Thank you

cross
  • 363
  • 1
  • 4
  • 15
  • 1
    How can it be any easier? It only takes 2 lines of code? There are many answers on SO about t his subject. You should try to implement one of them, and if you have a problem, ask a specific question about that. – rdelmar Mar 31 '15 at 19:27
  • Is it just the one variable that you want to pass between views? – Swinny89 Mar 31 '15 at 19:28
  • 1
    Using `prepareForSegue` **is** the easy way...if you are actually using segues to move between view controllers. Are you? (I assume that when you say "view" you actually mean "view controller". If not, then the answer is going to be very different.) – Phillip Mills Mar 31 '15 at 19:30
  • @Swinny89 There are multiple variables that would need to be past in-between. I am researching singleton classes now - if there is anything you think I should know please tell me. I should have made it clearer in my question that I would be needing to transfer multiple variables. Thanks – cross Mar 31 '15 at 19:45
  • I have updated my answer to show how you could pass multiple variables through prepareForSegue. If your variables don't have a great connection to each other then it can be counter intuitive to create a class for them. Passing a dictionary loaded with these variables can be a good solution for this. – Swinny89 Mar 31 '15 at 19:52

2 Answers2

0

If it just one variable that you are sending, i would use prepare for segue.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "IdentifierOfSegueGoesHere"
    {
        if let destinationVC = segue.destinationViewController as? OtherViewController{
            destinationVC.dictionary = self.dictionary
        }
    }
}

If you are passing a lot of data, e.g. a list of friends etc then i would create a singleton class that stores them that can be accessed from anywhere.

If you are passing multiple variables that don't have enough of a connection to create a singleton class then you can put them into a dictionary and pass the dictionary.

Swinny89
  • 7,273
  • 3
  • 32
  • 52
-3

This is not a recommended approach. If you are not a computer science purist you can use global variables. This by-passes protections offered by the compilation process.

Syed Tariq
  • 2,878
  • 3
  • 27
  • 37