SwiftUI Label for Button won't show text when the text is a class variable changed by a class function

143 views Asked by At

I have a class that has a text variable and a function that changes the text variable based on an integer variable thats also in the class. The text variable is used as the label for a button. When I do something to change the int variable I expect the text shown to change as well but it doesn’t. When I tried printing the text variable to see if it was changing along with the int it was but for some reason the text of the button just won't change.

import SwiftUI

class thing{
    var str: String
    var int: Int
    
    func change(){
        self.str = "\(int*5)"
    }
    
    init(str: String, int: Int) {
        self.str = str
        self.int = int
    }
}

struct ContentView: View {
    
    @State var cla: thing
    
    init(cla: thing = thing(str:"Hello World", int: 5)) {
        self.cla = cla
    }
    
    var body: some View {
        Button(){
            cla.change()
            print(cla.str)
        }label: {
            Text(cla.str)
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

This has been really puzzling me for a while and I'd really appreciate if anyone could help.

0

There are 0 answers