0

I am working on a Magic the Gathering Counter app. For my app, I want to click twentyLife(), thirtyLife(), or fourtyLife() to change the value of a variable in another view controller (i.e. FifthViewController.)

Second view controller code:

import UIKit


class SecondViewController: UIViewController {
    var vcFive = FifthViewController()

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
@IBAction func twentyLife() {
    vcFive.lifePoints.text = String(20)
    
}
@IBAction func thirtyLife() {
    vcFive.lifePoints.text = String(20)
    
}

@IBAction func fortyLife() {
    vcFive.lifePoints.text = String(20)
}

I try and call out the fifthViewController as a variable to change my life points text but it does not work.

Here is my code for the fifthViewController:

import UIKit

class FifthViewController: UIViewController {
    @IBOutlet weak var lifePoints: UILabel!
    var counter = 0
    

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

@IBAction func lifeUp() {

    counter += 1
    lifePoints.text = String(counter)
}

@IBAction func lifeDown() {
    counter -= 1
    lifePoints.text = String(counter)
}

Any help would be appreciated. This is my first app I have been trying to work on from scratch

1 Answers1

0

You should create an instance of your FifthViewController on your SecondViewController, and a variable on your FifthViewController in which you can save the data passed. Like this:

SecondViewController

import UIKit


class SecondViewController: UIViewController {
    
    let fifthVC = self.storyboard?.instantiateViewController(withIdentifier: "fifthVC") as? FifthViewController

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    @IBAction func twentyLife() {
        fifthVC.lifePointsInt = 20

    }

    @IBAction func thirtyLife() {
        fifthVC.lifePointsInt = 30

    }

    @IBAction func fortyLife() {
        fifthVC.lifePointsInt = 50
    }
}

FifthViewController

import UIKit

class FifthViewController: UIViewController {
    var lifePointsInt = Int()
    @IBOutlet weak var lifePoints: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()

        lifePoints.text = "\(lifePointsInt)"
    }

    @IBAction func lifeUp() {
        lifePoints.text = "\(lifePointsInt + 1)"
    }

    @IBAction func lifeDown() {
        lifePoints.text = "\(lifePointsInt - 1)"
    }
}

Remember to type in your Storyboard ID in your storyboard for your FifthViewController as "fifthVC" If your pass to your FifthViewController after selecting your Life Count in your SecondViewController you could add self.navigationController?.pushViewController(fifthVC!, animated: true) at the end of each function.

  • Thank you. I am now getting this error on my second view controller. Code: Value of type '(SecondViewController) -> () -> SecondViewController' has no member 'storyboard' – JustTrying2Learn Jul 22 '20 at 00:13
  • Is that on a specific line of code? Is your SecondViewController on your storyboard connected with your Swift file? – Santiago Fabregat Jul 22 '20 at 18:16