2
votes

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
5
Store variable in the object which both controllers have reference to.Jakub Vano

5 Answers

1
votes

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
}

//call
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.

1
votes

You could use notifications. I don't know how they work in Swift, but in Objective C you could post a VariableShouldIncreaseNotification and VariableShouldDecreaseNotification.

Then, you have either a third object that listens for these notifications and keeps track of the current value, or you could decide that one of the ViewControllers is the owner of the variable and it should listen for notifications from the other.

Either way the notifications keep the message sending decoupled from the message processing, so it's easier to move it around later if you need to.

1
votes

pass a reference your VC A that has the VARIABLE to B -- now B can work on A's property

if it is more complicated think about formalising the 'contract' a bit using a protocol that A conforms to

1
votes

Seeing as the accepted answer is terrible (in my opinion), I thought I'd give a more sensible answer for others.

Please note that this is not perfect either, but better. Create a variable within the initial view controller.

Next, within the tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) function create a switch case of indexPath.row like so (keep in mind cells start at index 0):

switch indexPath.row {
case 0:
  ...
default:
  ... 
}

Then assign the value you want to the variable you created before within one of these cases. Perform self.performSegueWithIdentifier and then add the function below:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "SegueIdentifier") {
        let destination = segue.destinationViewController as! DestinationViewControllerClass
        destination.variable = self.variable
    }

}

Obviously chaning "SegueIdentifer" with your identifier, DestinationViewControllerClass with the class you're using for the view controller you want to pass the variable to (in your case prizesViewController), and the variable names to your variable names (coins1 and self.coins2 respectively).

0
votes

One possible way to accomplish this is using a third class, which holds the variable. If you make this class a singleton, both view controllers will be able to access the same value.

Information on how to create singletons in Swift can be found on this Stack Overflow question. Assure yourself, however, to make the variable access thread safe.