UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SplitView switching detail view doesnt work

Forums > SwiftUI

Building SplitView app, when tapping on different cells in SplitView sidebar, it only works first time, second time, it never switches. Here is my source code.

//
//  JournalListView.swift
//  JournalApp
//
//  Created by Štěpán Pazderka on 01.12.2023.
//

import SwiftUI
import RealmSwift

struct JournalListView: View {
        @ObservedResults(JournalEntry.self) var journalEntries
//    var journalEntries = [JournalEntry(id: UUID(), name: "Test1", date: Date(), body: "Test1"), JournalEntry(id: UUID(), name: "Test2", date: Date(), body: "Test2")]

    @State private var selectedEntryID: UUID?
    @State private var showingAddNewJournalEntry = false

    var body: some View {
        NavigationSplitView {
            List {
                ForEach(journalEntries) { entry in
                    NavigationLink { JournalEntryView(entryToEdit: entry)} label: {
                        ListViewCell(entry: entry, selected: selectedEntryID?.uuidString == entry.id)
                    }
                    //                    .navigationDestination(for: JournalEntry.self) { entry in
                    //                        JournalEntryView(entryToEdit: entry)
                    //                    }
                }
                //                .onDelete(perform: $journalEntries.remove)
            }
            .navigationTitle("Lumi")
            .listRowInsets(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
            .toolbar {
                ToolbarItem(placement: .automatic) {
                    Button {
                        showingAddNewJournalEntry = true
                    } label: {
                        Image(systemName: "plus")
                    }
                }
            }
            .sheet(isPresented: $showingAddNewJournalEntry, content: {
                JournalEntryView()
            })
        } detail: {

        }
    }
}

#Preview {
    JournalListView()
        .environment(\.realm, DatabaseInteractor.RealmMockup)
}

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.