Help please

I have ViewController and file "Cake.swift"

In Cake I have var "buttonText" but when i make this

@IBAction func buttonClick(_ sender: UIButton) {
        var buttonTitle = sender.currentTitle!
        print(buttonTitle)
        Cake().buttonText = buttonTitle
        print(Cake().buttonText)
    }

the print(cake().buttonText) = nil

but buttonTitle = ship

what`s going up?

Replies

The problem is :


        Cake().buttonText = buttonTitle
        print(Cake().buttonText)



Line 1 creates a new instance of Cake, but with no var referncing

Then on secon line, you create another reference, different from the first.


You should write :


     var aCake = Cake()     // You have one instance
     aCake .buttonText = buttonTitle     // You set the property "buttonTitle"
     print(aCake.buttonText)     // You use the same instance

okay, but if i need to use the buttonTitle in another file? i have the same problem

The point is that you need to get access to the instance of the object.


And to understand which instance you want to call.


But for sure, not create a new instance each time.


Can you post the code of what you are doing in the other file ?

import ARKit

import UIKit

class Cake: SCNNode{

var buttonText = ""

func LoadModal(){

guard var cake = SCNScene(named: "art.scnassets/\(buttonText).scn") else {return}

var cakeNode = SCNNode()

for child in cake.rootNode.childNodes{

cakeNode.addChildNode(child)

}

self.addChildNode(cakeNode)

}

}

Well, that's the Cake class.


The point to see is the class where you call


aCake.buttonText = buttonTitle


But logically, in this class, the following should work:


    aCake .buttonText = buttonTitle     // You set the property "buttonTitle" 
     print(aCake.buttonText)     // You use the same instance

The question is : where is the Cake instance (aCake) defined ?