WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

NavigationSplitView questions

Forums > SwiftUI

I am trying to port an applicaiton I am working on for MacOS to use the new NavigationSplitViews.

Two issues have come up:

  1. When run I get this message repeatedly:

"Update NavigationAuthority bound selection tried to update multiple times per frame."

  1. The following is the heart of the navigation I am using (simplified). If my content is a TextEditor(...) it works correctly. If I use the NSTextView/Representable - CustomTextEdit in the code below it doesn't work. It did/does work with the older style of NavigationLinks.

The editor is initialized before any items in the list are clicked on and are not updated when a different row in the list is clicked. BUT. If I save the document, and reopen the saved, the clicked on row is presented. So, the issue is the NSTextView is not getting updated. Any suggestions would be greatly apprecaited.

Also, the Text view in the detail section is being updated corrected when rows in the sidebarlist are clicked on.

EDITED: I can see the $document.model.selected is not being updated on clicking/selecting a row.

struct ContentView: View {

    @Binding var document: HermesDocument

    var body: some View {

        NavigationSplitView(columnVisibility: $columnVisible) {
            if let children = document.model.sidebar.children {
                List(children, id: \.self, children: \.children, selection: $document.model.selected) { sidebar in
                    Label(sidebar.name, systemImage: SFSymbolsModel.icon[sidebar.iconName] ?? "questionmark")
                }
                .navigationSplitViewColumnWidth(min: 200, ideal: 250)
            }

        } content: {
            CustomTextEditor(text: $document.model.selected.text)
                .padding(.all)
                .frame(width: 600.0)

        } detail: {
            VStack {
                Text(document.model.selected.text?.string ?? "empty")
                    .padding(.all)
                    .frame(width: 200.0)
            }
        }
        .navigationSplitViewStyle(.prominentDetail)
    }
}

   

A bit of help from anyone that has used NSTextView: Representable.

Apparantly in the previous method of NavigationLink the view (CustomTextView: NSTextView, Representable) was destroyed and recreated everytime you clicked on a row in the list. Now, it appears to create the view when the ContentView is created, and then leaves that view alive. The NSTexstView Coordinator.updateNSView is getting called, but I don't know how to tell it to replace the Text.

Do I just place the new text into TextView like this, and if so, do I have to delete the old or will replacing it this way deal with the memory allocation? (Sorry, I am an old school C/C++ programmer, and I worry constantly about memory allocation/deallocation.)

        textContentStorage.textStorage?.setAttributedString(text!.aString)

   

Why do you have two nested NavigationSplitView in your code? And even if, the second var body: some View { should go away.

   

https://www.hackingwithswift.com/forums/swiftui/navigationsplitview-questions/14983/15010

Thanks that was a copy/paste error when posting. Apparently the browser didn't show the older version wasn't replaced.

Should be fixed now.

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain. The code can be hard to write, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app.

Explore the docs

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.