ios – How can I append a brand new worth to an current array from a distinct swiftui file?


I’ve obtained a venture the place I’ve a distinct view controller (a modual) the place once I press the “Add” button I might wish to append to a Record. I’ve the modual vc in a distinct SwiftUI file.

I used to be questioning how can I append to the already exisiting array from the completely different vc file. I’ve included my code from each information. Which incorporates what I’ve already tried.

ContentView File

`import SwiftUI


struct ContentView: View {
    struct Driving: Identifiable, Hashable{
        let title: String
        let id = UUID()
    }
    
    @State public var drivingList = [
        Driving(name: "Hello World"),
        Driving(name: "Matt")
    ]

    @State non-public var multiSelection = Set<UUID>()
    
    @State public var isShowingSheet = false
    
    var physique: some View {
        NavigationView {
            Record(drivingList, choice: $multiSelection){
                Textual content($0.title)
            }
            .navigationTitle("Driving Logbook")
            .toolbar {
                Button("Check Add with date", motion: {
                    addWithDate()
                });
                EditButton();
                Button(motion: {
                    isShowingSheet.toggle()
                },
                       label: {
                    Label("Add", systemImage: "plus.app")
                }).sheet(isPresented: $isShowingSheet, content material: {
                    AddLogbookView()
                });
            }
        }
    
        
    }
    
    func didDismiss(){
        
    }
    
    func addWithDate(){
        let dateNow = Date.now
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd/MM/YYYY"
        drivingList.append(Driving(title: dateFormatter.string(from: dateNow)))
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}`

AddLogbook SwiftUI File – Modual VC

`
import SwiftUI

struct AddLogbookView: View {
    @Surroundings(.dismiss) var dismiss
    @State non-public var sometext: String = ""
    
   @State public var pubDrivingL = ContentView().drivingList
    
    
    var physique: some View {
        NavigationView{
            TextField(
                "Check",
                textual content: $sometext
            )
                .navigationBarTitle("Add Log", displayMode: .inline)
                .toolbar{
                    ToolbarItem(placement:.navigationBarLeading){
                        Button(motion: {
                            ContentView().isShowingSheet.toggle()
                            dismiss()
                        }, label: {
                            Textual content("Cancel").daring()
                        })
                    }
                    ToolbarItem(){
                        Button(motion: {
                            pubDrivingL.append(ContentView.Driving(title: "Check"))
                            print("View")
                            ContentView().isShowingSheet.toggle()
                            dismiss()
                            
                        }, label: {
                            Textual content("Add").daring()
                        })
                    }
                }
        }
    }
}

struct AddLogbookView_Previews: PreviewProvider {
    static var previews: some View {
        AddLogbookView()
    }
}
`

I’ve tried referencing the array in a var or simply by merely referencing the ContentView vc after which the array itself. I used to be anticipating that once I press the “Add” button it will append a worth to the exisiting array.

Clearly I am lacking one thing right here, and am at the moment studying SwiftUI, so if doable would love if solutions could possibly be defined if doable please and thanks!!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles