Really? You've tried everything possible?
@twostraws has a fantastically free course online. Changing button labels is covered in his course! The article you reference was written in 2019. Why are you not following Hacking With SwiftUI course? It's up-to-date!
What day are you on? Let us know what lesson needs more clarification. We're happy to help you.
In the meantime paste this into Playgrounds. Give it a go.
Think of buttonIsLocked
as a light switch in your flat. Is it on? Is it off? If the light switch is on, what do you want the button to look like? If is is off, answer the same question. What do you want the button to look like?
SwiftUI is a declarative framework. TELL SwiftUI what you want the buttons to look like if the light switch is on! Tell SwiftUI how to draw the button if the light switch is off.
Your next task is to come back here and tell us what you learned.
// Change button text and icon based on View's state
// For: @karelszo 2022.01.31
// By: Obelix
import SwiftUI
import PlaygroundSupport
struct LockButton: View {
@State private var buttonIsLocked = true
var buttonImage: String {
// Compute the image based on the button state
buttonIsLocked ? "lock" : "lock.open.fill" // Which image is returned?
}
var buttonLabel: String {
// Compute the label based on button state
buttonIsLocked ? "Locked" : "Unlocked"
}
var body: some View {
Button {
buttonIsLocked.toggle() // DECLARE what you want to do
} label: {
// DECLARE what you want to see
Label(buttonLabel, systemImage: buttonImage)
.foregroundColor(.white)
}
.frame(width: 180)
.padding()
.background(.teal)
}
}
PlaygroundPage.current.setLiveView(LockButton().frame(width: 300, height: 400))