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

Change text klick button...

Forums > Swift

Hello. Does anyone know how to write the code so that after each click on the button the text changes to two labels, for example "Open, closed"? I have already tried everything possible, for example @State var status = ["Open", "Closed"] but then how to continue to write in the button code? Or use enum ... case? Or ForEach? For an example I would need this see the link "Change name": https://betterprogramming.pub/what-is-state-in-swiftui-and-how-to-use-it-cc672de85188 Thank you

3      

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))

3      

Extra Credit Discussion:

For boolean variable names, some programmers use the ask-a-question convention. For example:

// This is a boolean. Variable name is the ask-a-question convention.
var isTheKitchenLightOn = true

Other programmers favor the make-a-statement convention, like this:

// Again, a boolean. Variable name is the make-a-statement convention.
var kitchenLightIsOn = true

Which convention do you favor?


Which feels more natural in an If statement?

// Use the make-a-statement boolean in an IF statement
if kitchenLightIsOn { 
    // do amazing stuff here 
} 
// Use the ask-a-question boolean here
if isTheKitchenLightOn {
    // do amazing stuff here
}

Which reads more naturally in a ternary operation?

// Use the make-a-statement convention in a ternary operation
kitchenLightIsOn ? "Very bright!" : "Too dark."

// Use the ask-a-question convention in a ternary operation
isTheKitcheLightOn ? "Very bright!" : "Too dark."

Discuss!

3      

Not sure if wanted it in UIKit or SwiftUI (as you posted in Swift but gave link to a SwiftUI

You can do just about anything you want with a button. Check these out (it in Hacking with Swift Plus section but you can get a free period)

Basic button customization using ButtonStyle

Advanced button customization using ButtonStyle

Animating buttons using ButtonStyle

Creating completely custom buttons using PrimitiveButtonStyle

You can get some effects like this

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.