Can I stock a value in a component (SwitUI View) and read it from another view ? (Lets say, one component value is "4" and a second component value is "6", I want to be able to read these values in order to display a total of "10")
See the answers at the below url. Hope that helps you get started.
How to access to a variable in another struct? SWIFTUI
How can I append a component into my HStack when I click my button ?
You can have a ForEach that loops a card array variable (as State or ObservableObject) to create the CardView. Appending to the array, would refresh the view. Here's some example code.
import SwiftUI
class Card: NSObject {
var id: Int
var name: String
init(id: Int, name: String) {
self.id = id
self.name = name
}
}
class Model: ObservableObject {
@Published var cards: [Card] = []
init() {
cards = [
Card(id: 1, name: "Card 1"),
Card(id: 2, name: "Card 2"),
Card(id: 3, name: "Card 3")
]
}
}
struct ContentView: View {
@ObservedObject var model = Model()
var body: some View {
NavigationView {
List {
ForEach(model.cards, id: \.self) { card in
Text("\(card.name)")
}
Spacer()
}
.padding()
.navigationBarItems(trailing:
Button(action: self.addCard) {
Image(systemName: "plus")
}
)
}
.navigationBarTitle("Cards")
}
private func addCard() {
let id = model.cards.count + 1
model.cards.append(Card(id: id, name: "New Card \(id)"))
}
}