Matthew Yue
Matthew Yue

Reputation: 11

Swift Game How to call a variable from a different view controller class

I made this card game app. So at first the game randomizes four cards that will be later used in a different view controller. My second view controller will display and use the randomized cards. What I did is that I have a arc4random function in the first view controller and set it to a variable, then I want to use these variables to be displayed in the second view controller. The problem is that I can't use the variable from one view controller to the other view controller.

Here is my randomize var in the first view controller:

    @IBAction func randomizeButtonTapped(_ sender: UIButton) {

    //randomize function
    var firstNumber =  arc4random_uniform(13) + 1
    firstCardImageView.image = UIImage(named: "card\(firstNumber)")

    var secondNumber = arc4random_uniform(13) + 1
    secondCardImageView.image = UIImage(named: "card\(secondNumber)")

    var thirdNumber = arc4random_uniform(13) + 1
    thirdCardImageView.image = UIImage(named: "card\(thirdNumber)")

    var fourthNumber = arc4random_uniform(13) + 1
    forthCardImageView.image = UIImage(named: "card\(fourthNumber)")

and here is the code on my second view controller:

    @IBAction func firstButtonTapped(_ sender: UIButton) {

    label.text = label.text + String("\(firstNumber)")
}

Upvotes: 1

Views: 80

Answers (3)

Zach Fuller
Zach Fuller

Reputation: 1269

One way you could go about it is to create a helper class that handles the randomization of the cards. The same numbers would then be available in different view controllers. Something like this:

class CardService {

    static let instance = CardService()
    private(set) var cards = [UInt32]()

    func randomizeCards() {
       cards.removeAll()
        for _ in 0...3 {
            let num = arc4random_uniform(13) + 1
            cards.append(num)
        }
    }
}

Then in the first view controller when the button is tapped call:

CardService.instance.randomizeCards()

In the view controller in which you want to use the numbers you could access them by index like so:

CardService.instance.cards[0]

The benefit here is you can access the numbers from any view controller with or without a segue and can also randomize the cards from any view controller without duplicating the function that handles randomization.

Upvotes: 0

Tung Fam
Tung Fam

Reputation: 8167

In your FirstVC add the variable on top of your class as a global variable:

var cards = [Int]()

In your SecondVC create the same variable:

var cards = [Int]()

Add this method in your FirstVC:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if let secondVC = segue.destination as? YourSecondVC {
        secondVC.cards = self.cards
    }
}

This method will be called right before it will show the SecondVC. And basically it assigns the cards from the 1st VC to the cards of the 2nd VC.

Then your method will look like this:

@IBAction func firstButtonTapped(_ sender: UIButton) {
   label.text = label.text + String("\(cards[0])")
}

Upvotes: 1

the_kaseys
the_kaseys

Reputation: 274

You need to use this function, and then set up a segue from your first view controller to the second view controller with an identifier.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    switch segue.identifier {
    case "YOUR_SEGUE_IDENTIFIER"?:
        let destination = segue.destination as! UIViewController()
        destination.VARIABLE_NAME_ON_NEXT_VC = VARIABLE_YOU_ARE_PASSING
    default:
        print("dont know where to send in segue")
    }
}

Upvotes: 0

Related Questions