13 C
London
Thursday, September 26, 2024

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


I’ve bought a undertaking the place I’ve a special view controller (a modual) the place once I press the “Add” button I might wish to append to a Checklist. I’ve the modual vc in a special SwiftUI file.

I used to be questioning how can I append to the already exisiting array from the totally 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 identify: 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 {
            Checklist(drivingList, choice: $multiSelection){
                Textual content($0.identify)
            }
            .navigationTitle("Driving Logbook")
            .toolbar {
                Button("Take a look at 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(identify: 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(
                "Take a look at",
                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(identify: "Take a look at"))
                            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 could append a worth to the exisiting array.

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

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here