Tuesday, May 19, 2026
HomeiOS Developmentios - uniquely get Button if I extract as a subview...

ios – uniquely get Button if I extract as a subview in SwiftUI

[ad_1]

So I’m making a easy restaurant invoice splitting app and I’ve a button that I need to spotlight when the person clicks on it. I additionally need the opposite buttons to be not highlighted. In UIKit it will be easy to do that utilizing sender.currentTitle.

Right here is my code for the button

struct TipButton: View, Identifiable {
    var id: Int
    
    
    var tipPercentage: String
    @State var didTap: Bool = false
    @State var buttonLetter: String
    @State var zeroPctButton: Bool = false
    @State var tenPctButton: Bool = false
    @State var twentyPctButton: Bool = false

    
    var physique: some View {
        Button {
            
            print("Tip is... (Float(tipPercentage) ?? 7.7)")
            didTap.toggle()
            
            
            if buttonLetter == "A" {
                zeroPctButton = true
                tenPctButton = false
                twentyPctButton = false
                
            }
            else if buttonLetter == "B" {
                didTap = true
                
            }
            
            
        } label: {
            Textual content("(tipPercentage)%")
                .font(.largeTitle)
                .daring()
        }
        .background(didTap ? Shade.inexperienced : Shade.clear)
    }
}

To date I have been taking part in round with it and including various things like Identifiable and @State variables for the proportion quantities however cannot determine this out.

In my fundamental file I’d have the view constructed together with these buttons like

struct ButtonsView: View {

var physique: some View { 
    //blah blah 
    //some UI arranging code

    TipButton(id: 1, tipPercentage: "0", buttonLetter: "A")
    TipButton(id: 2, tipPercentage: "10", buttonLetter: "B")
    TipButton(id: 3, tipPercentage: "20", buttonLetter: "C")
}

}

As you’ll be able to see, I’ve tried id and buttonLetter

Briefly, I need to click on on button A, have it spotlight, then once I click on button B, it highlights and button A is not highlighted

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments