UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Update Text cause value Haus changed in SwiftUI

Forums > SwiftUI

import SwiftUI
import PlaygroundSupport
import Foundation

struct ContentView: View {
    @State var minutesOfTraining = 10
    var body: some View {
        VStack {
            HStack {
                Button(action: {
                    self.minutesOfTraining -= 1
                }) {
                    Text("-")
                }

                Button(action: {
                    self.minutesOfTraining += 1
                }) {
                    Text("+")
                }

            }
            Text("\(minutesOfTraining) min.")
        }
    }
}

PlaygroundPage.current.setLiveView(ContentView())

I want to update the Text() to the current value of minutesOfTraining, after changing it with the button. What can i do?

3      

You don't need to do anything. By virtue of minutesOfTraining being an @State property, it will automatically update the Text in which it is displayed.

playground button

Hmm... for some reason the image is not embedding properly. Oh well, you can click on it to see a GIF of the code working.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.