nathan nathan - 6 months ago 65
iOS Question

Pass data between view controllers constantly without a segue? USING SWIFT

Note: I am trying to solve this problem with swift, not objective c.

I am making an app the has 2 view controllers, one of the view controllers job is to deduct points from the variable, and the other view controllers job is to add points to that variable.

How can i make it that both view controllers share the same variable, or just constantly pass the variable between them?

Note, the 2 view controllers are connected with a tab bar controller, so i cant use a segue. I have tried using this code, but it does not work. Any Suggestions?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
var destviewcontroller: prizesViewController = segue.destinationViewController as prizesViewController
//first other coins them mine
destviewcontroller.coins1 = coins2


You could make a global variable in it's own file and call the variable in both view controllers:

struct Variables {
    static var coinsVariable = 0

Variables.coinsVariable = 10

Also you could make a protocol where you set a function to update the coinValue. Then you can use it inside your ViewControllers as delegate and get the value from the method in the delegate. Check the Apple Documentation how to use protocols.