Store int and call it in another file

This is my code:

var art: Int = 0
@IBAction func art(_ sender: Any) {
        art += 1
        tutText = "artTut"
        if art == 1{
            print(art)
            let storyBoard: UIStoryboard = UIStoryboard(name: "Art", bundle: nil)
            let newViewController = storyBoard.instantiateViewController(withIdentifier: "UITabBarController") as! NewViewController
            self.present(newViewController, animated: true, completion: nil)
        }
    }

The log printed '1'. But when I proceed to NewViewController, the log printed '0' See the code in NewViewController.:

let main = PreviousViewController()
print(main.art)

Do you have solution so that it printed '1' in NewViewController

Your current code is is failing because this line:

let main = PreviousViewController()

doesn’t get a reference to the parent view controller but rather creates a new instance of

PreviousViewController
. The standard way of doing this sort of thing is dependency injection, that is, having the parent view controller push values into the child view controller. This might look something like this:
class NewViewController … {
    var art2: Int = 0

    … other stuff …
}

class PreviousViewController … {

    var art1: Int = 0  

    @IBAction func art(_ sender: Any) {  
        art1 += 1  
        tutText = "artTut"  
        if art1 == 1 {  
            let storyBoard: UIStoryboard = UIStoryboard(name: "Art", bundle: nil)  
            let newViewController = storyBoard.instantiateViewController(withIdentifier: "UITabBarController") as! NewViewController  
            newViewController.art2 = art1
            self.present(newViewController, animated: true, completion: nil)  
        }  
    }  

    … other stuff …
}

In this example I’ve given the properties different names because they are different values, making it easy to understand how the

art(_:)
method copies the value from itself (the parent view controller) to the child view controller.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you for the answer and I've tried solution but still not help, the log still printed "0". Do you have any other solutions?

The solution I posted should work. Can you post some snippets of your attempt to apply it?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Store int and call it in another file
 
 
Q