The plus and minus on the first two Views dont work only the last one. The buttons on I have a view that is inside a ForLoop all the information in the View is coming from a Data struct. I feel as if Ive narrowed the problem down to the fact that the far most view is the in the ForLoop is the “Original” instance
// Home page
//
// ContentView.swift
//
//
// Created on 5/25/24.
//
import SwiftUI
struct Home: View {
@State private var data: [Dataa] = MockData.data
var body: some View {
NavigationStack{
ScrollView(.horizontal){
VStack{
HStack{
ForEach(data.indices, id: .self){ num in
VStack{
CounterView(data: $data[num])
}
}
}
}
}
.navigationTitle("Home")
.toolbar{
ToolbarItem(placement: .topBarLeading){
Image("placeholder")
.resizable()
.frame(width: 65, height: 65)
.cornerRadius(3)
}
}
}
}
}
#Preview {
Home()
}
// Counter View
//
// CounterView.swift
//
//
// Created on 5/26/24.
//
import SwiftUI
struct CounterView: View {
@Binding var data: Dataa
var body: some View {
ZStack{
Rectangle()
.frame(width: 200, height: 275)
.cornerRadius(20)
.foregroundColor(.indigo)
VStack{
Image("placeholder")
.frame(width: 100, height: 100)
.scaledToFill()
.clipShape(Circle())
Text("placeholder")
.foregroundColor(.gray)
.padding(.vertical, 35)
HStack{
// Minus Button
Button{
self.data.numero -= 1
} label: {
Image(systemName:"minus")
.foregroundColor(.gray)
.frame(width: 20, height: 20)
}
// Rectangles and TextFields
VStack{
ZStack{
Rectangle()
.frame(width: 105, height: 30)
.cornerRadius(5)
Text("(self.data.numero)")
.foregroundColor(.white)
.frame(width: 105, height: 30)
}
ZStack{
Rectangle()
.frame(width: 105, height: 30)
.cornerRadius(5)
Text("(self.data.numero)")
.foregroundColor(.white)
.frame(width: 105, height: 30)
.clipped()
}
}
// Minus Button
Button{
self.data.numero += 1
} label: {
Image(systemName:"plus")
.foregroundColor(.gray)
.frame(width: 20, height: 20)
}
}
}
}
}
}
#Preview {
CounterView(data:.constant(Dataa(numero: 0, image: "")))
}
// Data
//
// Data.swift
//
//
// Created on 5/26/24.
//
import Foundation
import SwiftUI
struct Dataa: Hashable, Identifiable {
let id = UUID()
var numero: Int
let image: String
}
struct MockData{
static let sampleView = Dataa(numero: 0, image: "placeholder")
static let data = [
Dataa(numero: 3, image: "placeholder"),
Dataa(numero: 2, image: "placeholder"),
Dataa(numero: 1, image: "placeholder")]
}
Ive tried a variety of things. At first I thought it was a UI bug and spent 30 minutes trying to debug my UI. After this it was a jumble of things i kept on trying