0

The following code:

struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
        animation: .default)
    private var items: FetchedResults<Item>
    var body: some View {
        NavigationView {
            List {
                ForEach(items) { item in
                    NavigationLink {
                        Text("Item at \(item.timestamp!, formatter: itemFormatter)")
                    } label: {
                        Text(item.timestamp!, formatter: itemFormatter)
                    }
                }
                .onDelete(perform: deleteItems)
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    EditButton()
                }
                ToolbarItem {
                    Button(action: addItem) {
                        Label("Add Item", systemImage: "plus")
                    }
                }
            }
            Text("Select an item")
        }
    }
    private func addItem() {
        withAnimation {
            let newItem = Item(context: viewContext)
            newItem.timestamp = Date()
            do {
                try viewContext.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nsError = error as NSError
                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
            }
        }
    }
    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            offsets.map { items[$0] }.forEach(viewContext.delete)
            do {
                try viewContext.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nsError = error as NSError
                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
            }
        }
    }
}
private let itemFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    formatter.timeStyle = .medium
    return formatter
}()
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
    }
}

Renders to this:

enter image description here

I would like the details to default to the first item in the list by default when the app first runs, but only if the details is showing, so, on an iPhone for example I don't want it to be pushed automatically.

How can the details screen automatically show the first item in the list and not say "Select an item" when master-details is showing and not just master (left view)?

zumzum
  • 17,984
  • 26
  • 111
  • 172
  • Does this answer your question https://stackoverflow.com/a/63230898/12299030? – Asperi May 04 '22 at 16:17
  • So I tried a hidden navigation link approach and it works. What I am not sure of is how to know if the details screen (the right side) is showing because I don't want to trigger the navigation if collapsed. – zumzum May 04 '22 at 16:29
  • You can't control that. The OS will actually follow the links on the view and try to preemptively create the linked views. You can see it if you put breakpoints in and step through. You can get some unexpected crashes if you don't consider that in advance. – Yrb May 04 '22 at 18:15
  • I am not sure if the settings app on iOS uses SwiftUI but, that is basically what I'm trying to recreate. Settings app does not open blank in details on iOS ... – zumzum May 05 '22 at 13:29
  • You said that the link that Asperi gave worked. I am not sure what, exactly, you are having a problem with. Do you think on an iPhone that the link will push simply because the view has been initialized already? It won't, but it will be ready to without delay. – Yrb May 05 '22 at 14:08

0 Answers0