WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

1      

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

1      

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!

1      

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

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.