Buttons in my navigationViewItems are not working until I use the navigation view links

I've been working on a small app using SwiftUI and as part of my app I have a custom slide up modal with a NavigationView inside it (so I can use pickers).


I have two buttons in my navigationViewItems neither of which are working as they should be until you use the picker.


See the attached video:


https://www.youtube.com/watch?v=D4vB_p9HDl8&feature=youtu.be


struct AddView: View {
    var contentMain: ContentView
    
    @State private var dueDate = Date()
    @State private var description = String()
    @State private var selectedTeacher = 0
    @State private var extra = String()
    
    
    
    var body: some View {
        NavigationView {
            
            Form {
                Section(header: Text("General Settings")){
                    DatePicker(selection: $dueDate, in: Date()..., displayedComponents: .date) {
                        Text("Select due date")
                    }
                    Picker("Teacher", selection: $selectedTeacher) {
                        ForEach(0 ..< openTeachers().sorted().count) {
                            Text("\(openTeachers().sorted()[$0].name) - \(formatSubject(subj: openTeachers().sorted()[$0].subject))")
                        }
                    }
                }
                Section(header: Text("About")){
                    TextField("Description", text: $description)
                    TextField("Extra", text: $extra).lineLimit(12)
                }
            }.navigationBarTitle("Add new homework", displayMode: .inline).navigationBarItems(leading: Button("Cancel") {
                print("clicked")
                self.contentMain.dropAddMenu()
                self.dueDate = Date()
                self.description = ""
                self.selectedTeacher = 0
                self.extra = ""
                self.contentMain.enableSettings()
            }.padding(.horizontal, CGFloat(10)).padding(.vertical, CGFloat(2)).font(.headline).foregroundColor(.white).background(Color.red).cornerRadius(CGFloat(40))
                , trailing:Button("Done") {
                    self.contentMain.dropAddMenu()
                    self.contentMain.enableSettings()
                    print("")
                }.padding(.horizontal, CGFloat(10)).padding(.vertical, CGFloat(2)).font(.headline).foregroundColor(Color.white).background(Color.blue).cornerRadius(CGFloat(40)))
        }
    }
}


I know the code isn't the best possible so feedback is welcome there but I'm stumped as to where to progress to fix this bug, any help would be appreciated. Thank you!

Accepted Reply

Nevermind, appeared to have fixed it by dropping the custom modal implementation and opting for a sheet.

Replies

Nevermind, appeared to have fixed it by dropping the custom modal implementation and opting for a sheet.