ios – SwiftUI and Core Knowledge


I am experimenting with Core Knowledge and SwiftUI. I can create and replace an entry however when I attempt to delete, one thing unusual occurs. The mannequin is a straightforward Entity with a “title”property.

image

I am utilizing the brand new NavigationSplitView as Apple counsel https://developer.apple.com/documentation/swiftui/migrating-to-new-navigation-types with sidebar and element.

import SwiftUI

struct ContactsListView: View {
    @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: Person.name, ascending: true)], animation: .default)
    non-public var contacts: FetchedResults<Individual>
    
    @State non-public var isAddContactPresented = false
    @State non-public var isEditContactPresented = false
    @State non-public var choice: Individual?
    
    var physique: some View {
        NavigationSplitView {
            Listing(contacts, choice: $choice) { contact in
                NavigationLink(worth: contact) {
                    Textual content(contact.title ?? "No title")
                }
            }
            .navigationTitle("Contacts")
            .toolbar {
                ToolbarItem(placement: .primaryAction) {
                    Button {
                        isAddContactPresented.toggle()
                    } label: {
                        Picture(systemName: "plus")
                    }

                }
            }
            .sheet(isPresented: $isAddContactPresented) {
                AddContactView()
            }
        } element: {
            if let contact = choice {
                VStack {
                    Textual content(contact.title ?? "No title")
                    
                    Button {
                        isEditContactPresented.toggle()
                    } label: {
                        Textual content("Edit")
                    }
                    .sheet(isPresented: $isEditContactPresented) {
                        AddContactView(contact: contact)
                    }

                }
            } else {
                Textual content("Create a brand new contact")
            }
        }

    }
}

In AddContactView I am detecting if is a brand new contact or an present contact.

struct AddContactView: View {
    @Atmosphere(.managedObjectContext) var viewContext
    @EnvironmentObject var dataController: DataController
    
    @Atmosphere(.dismiss) var dismiss
    
    @State non-public var title: String = ""
    
    let contact: Individual?
    
    init(contact: Individual? = nil) {
        self.contact = contact
        
        if let existingContact = contact {
            _name = State(wrappedValue: existingContact.title ?? "")
        }
    }
    
    var physique: some View {
        NavigationStack {
            Type {
                Part {
                    TextField("title", textual content: $title)
                    
                    Button("Save") {
                        saveContact()
                    }
                }
                }
            .navigationTitle("Add/Edit View")
            .toolbar {
                if (contact != nil) {
                    ToolbarItem(placement: .primaryAction) {
                        Button {
                            if let contact = contact {
                               attempt? dataController.delete(contact)
                                dismiss()
                            }
                        } label: {
                            Picture(systemName: "trash")
                                .foregroundColor(.crimson)
                        }

                    }
                }
            }
        }
    }
    
    func saveContact() {
        if let existingContact = self.contact {
            existingContact.title = title
        } else {
            let contact = Individual(context: viewContext)
            contact.title = title
        }
        
        attempt? viewContext.save()
        dismiss()
    }
}

The issue is once I faucet on the delete button in AddContactView, the contact is efficiently deleted however the detailView just isn’t dismissed and title it is changed by the nil coalescing string. How can I dismiss the View on iPhone and clear the element on iPad?

This is a pattern video

https://youtu.be/Bak7UR9kxKs

Leave a Reply