HomeiOS Developmentios - SwiftUI tapping a Button at all times selects the final...

ios – SwiftUI tapping a Button at all times selects the final button in a view


I posted an analogous query final night time and obtained a number of feedback from customers who couldn’t reproduce the problem. Under is an MRE (actually a minimal unreproducible instance) which I assume is analogous to what these customers examined:

import SwiftUI

@fundamental
struct ColorPickerTestApp: App {
    @StateObject non-public var accentColorManager = AccentColorManager()

    var physique: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(accentColorManager)

        }
    }
}

class AccentColorManager: ObservableObject {
    
    let colours: [Color] = [.orange, .yellow, .green, .blue, .purple, .brown, .cyan, .indigo, .mint, .teal]

    @Printed var accentColor: Coloration = .orange // on preliminary launch orange is default colour

    @Printed var accentColorIndex: Int = 0 {
        didSet {
            accentColor = colours[accentColorIndex]
            saveAccentColorIndex()
            objectWillChange.ship()
        }
    }

    init() {
        loadAccentColorIndex()
    }
    
    non-public let accentColorIndexKey = "AccentColorIndex"
    
    non-public func saveAccentColorIndex() {
        UserDefaults.normal.set(accentColorIndex, forKey: accentColorIndexKey)
    }

    non-public func loadAccentColorIndex() {
        if let storedIndex = UserDefaults.normal.worth(forKey: accentColorIndexKey) as? Int {
            accentColorIndex = storedIndex
        }
    }
}

struct ContentView: View {
    @EnvironmentObject var accentColorManager: AccentColorManager

    var physique: some View {
        VStack {
            Picture(systemName: "globe")
                .imageScale(.massive)
                .foregroundStyle(accentColorManager.accentColor)
            Textual content("Good day, world!")
                .foregroundStyle(accentColorManager.accentColor)
            Spacer()
            ActiveColorSectionView()
        }
        .padding()
    }
}

struct ActiveColorSectionView: View {
    @EnvironmentObject var accentColorManager: AccentColorManager

    var physique: some View {
        Part(header: Textual content("Lively Coloration")) {
            VStack(alignment: .main) {
                ForEach(accentColorManager.colours.indices, id: .self) { index in
                    Button(motion: {
                        accentColorManager.accentColorIndex = index
                    }) {
                        HStack {
                            Textual content(accentColorManager.colours[index].description)
                            Spacer()
                            Circle()
                                .body(width: 20, top: 20)
                                .foregroundColor(accentColorManager.accentColorIndex == index ? accentColorManager.colours[index] : .clear)
                                .overlay(
                                    Circle()
                                        .stroke(Coloration.main, lineWidth: 2)
                                )
                        }
                    }
                    .foregroundColor(accentColorManager.colours[index])
                    .padding(.vertical, 5)
                }
            }
        }
        .listRowBackground(Coloration(UIColor.systemBackground))
    }
}

This works fantastically. What I can’t determine is why the precise code behaves in another way within the context of my app. In my app, choosing a button from the record of colours at all times leads to each single button current within the view (no matter whether or not a ForEach is used, or if I manually code a number of buttons) being pressed till finally the ultimate button is pressed ensuing within the closing colour displayed within the record being set because the accent colour in my app.

Initially I assumed this was an issue with a closure seize associated to the ForEach however I intentionally eliminated the ForEach and used a VStack of buttons and the identical subject continued: the ultimate button within the VStack was at all times chosen, to not point out the ForEach works as anticipated within the MRE.

Debugging my mission I can press a button after which watch as all buttons are sequentially “pressed” in my mission. Can anybody present recommendations as methods to additional debug and/or resolve this subject?

Extra code and context describing my app:

My app’s fundamental display has an icon which navigates to a settings view:

struct SettingsView: View {
    @StateObject var savedLocations = SavedLocations()
    @ObservedObject var networking: Networking
    @EnvironmentObject var accentColorManager: AccentColorManager
    
    var physique: some View {
        VStack {
            Type {
                FrostAlertsSectionView(savedLocations: savedLocations, networking: networking)
                ActiveColorSectionView()
            }
            
        }
        .navigationTitle("Settings")
        .accentColor(accentColorManager.accentColor)
    }
}

Other than that my app accommodates one dozen views every anticipating the AccentColorManager Atmosphere Object. A hyperlink to a department on GitHub that I count on will reproduce the problem (has my present damaged code) is right here.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments