Cannot add something to an Array

Viewed 102

i have made an app in SwiftUI where you can create different classes. The app saves this in an array. I have a textfield and a button in the same view as the scrollview that displays the array. This works perfectly fine and I can easily add new classes. Now I made a new view with a text field and a button. This view can be viewed by pressing a button in the nav bar. It uses the exact same function as the other view, but in the other view adding a item to the array works, in this view it doesn't work. I hope you understand my problem and can help me. Thank You.

This is the file where I store the array:

import Foundation
import SwiftUI
import Combine

struct Class: Identifiable {
    var name: String
    var id = UUID()
}

class ClassStore: ObservableObject {
    @Published var classes = [Class]()
}

This is the view with the button + textfield that works and the scrollview that displays the array:

import SwiftUI

struct ContentView: View {
    @State var showNewClass = false
    @ObservedObject var classStore = ClassStore()
    @State var newClass: String = ""
    @State var toDoColor: Color = Color.pink
    
    func addNewClass() {
        classStore.classes.append(
            Class(name: newClass)
        )
        newClass = ""
    }
    
    
    var body: some View {
        NavigationView {
            VStack {
                HStack {
                    TextField("New Todo", text: $newClass)
                    Image(systemName: "app.fill")
                        .foregroundColor(Color.pink)
                        .padding(.horizontal, 3)
                    Image(systemName: "books.vertical")
                        .padding(.horizontal, 3)
                    if newClass == "" {
                        Text("Add!")
                            .foregroundColor(Color.gray)
                    } else {
                        Button(action: {
                            addNewClass()
                        }) {
                            Text("Add!")
                        }
                    }
                }.padding()
                ScrollView {
                    ForEach(self.classStore.classes) { name in
                        Text(name.name)
                    }
                }
                .navigationBarTitle(Text("Schulnoten"))
                .navigationBarItems(trailing:
                Button(action: {
                    self.showNewClass.toggle()
                }) {
                    Text("New Class")
                }
                .sheet(isPresented: $showNewClass) {
                    NewClass(isPresented: $showNewClass)
                })
            }
        }
    }
}

And this is the new view I created, the button and the textfield have the exact same code, but somehow this doesn't work:

import SwiftUI

struct NewClass: View {
    @Binding var isPresented: Bool
    @State var className: String = ""
    @ObservedObject var classStore = ClassStore()
    
    func addNewClass() {
        classStore.classes.append(
            Class(name: className)
        )
    }
    
    var body: some View {
        VStack {
            HStack {
                Text("New Class")
                    .font(.title)
                    .fontWeight(.semibold)
                Spacer()
            }
            TextField("Name of the class", text: $className)
                .padding()
                .background(Color.gray)
                .cornerRadius(5)
                .padding(.vertical)
            Button(action: {
                addNewClass()
                self.isPresented.toggle()
            }) {
                HStack {
                    Text("Safe")
                        .foregroundColor(Color.white)
                        .fontWeight(.bold)
                        .font(.system(size: 20))
                }
                .padding()
                .frame(width: 380, height: 60)
                .background(Color.blue)
                .cornerRadius(5)
            }
            Spacer()
        }.padding()
    }
}

Sorry if my English is not that good. I'm not a native speaker.

1 Answers

I assume you should pass same class store from parent view into sheet, ie

struct NewClass: View {
    @Binding var isPresented: Bool
    @State var className: String = ""
    @ObservedObject var classStore: ClassStore   // << expect external

and in ContentView

.sheet(isPresented: $showNewClass) {
    NewClass(isPresented: $showNewClass, classStore: self.classStore) // << here !!
})
Related