Here is the Updated ContentView. I set the StateObject to be the reader and routed the data class instructions through the reader class by adding reader. in from of data. Am I interpreting what you said wrong?
struct ContentView: View {
//@StateObject var data = Data()
@StateObject var reader : NFCReader = NFCReader()
var body: some View {
NavigationView {
List {
Section(
header: Text("\(reader.data.itemCount)" + " Items")
) { ForEach(reader.data.products, id: \.self) {
product in Text(product)
}
.onDelete(perform: reader.data.deleteItem)
.onMove(perform: reader.data.moveItem)
}
}
.navigationTitle("My Products")
.navigationBarItems(leading: EditButton(), trailing: AddButton)
.navigationViewStyle(.stack)
}
.environmentObject(reader.data)
}
var AddButton: some View {
Button(action: {
reader.activate()
// data.addItem()
// Next, once reader.activate() is done and sends data the tagUID, i need to open a new view with text imputs for name and description of product.
}, label: {
Image(systemName: "iphone.radiowaves.left.and.right")
})
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
// Do I need EnvironmentObject here???
.environmentObject(Data())
}
}
Post
Replies
Boosts
Views
Activity
I tried that and it still does not seem to work. It is as if ContentView only takes a snapshot of the Data class when the app boots up and that's it. I there a function I could create to update the view once something is added to the array? ContentView just doesn't know when Data is updated.
I was looking into completion handlers but the problem is that I would need to define ContentView in the NFCReader class and when I do that the program stops responding so I assume that it does not like that. How would I get around this?