TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Selected item on Sidebar forced back to nil when returning from Detail view (on iPhone)

Forums > SwiftUI

I have a NavigationSplitView with a Sidebar and Detail view. When selecting an item from the sidebar, the Detail view is updated to show what was selected. This all works fine so far on both an iPhone and an iPad.

However, on an iPhone, when returning back to the sidebar view to select something else, the previously selected item is being set to nil.

On an iPad it is not setting the previously selected item to nil, even if you hide and show the sidebar, it still remembers the previously selected entry.

I have tried with the iPhone simulator and on a physical device too.

To reproduce the issue, select an entry from the sidebar, the detail is shown/updated showing what was selected. Return back to the sidebar view and you can see for about half a second the previously selected item, then it gets set to nil.

Why is the selection being reset back to nil on the iPhone when returning back to the Sidebar view?

Please see very small complete code below:

import SwiftUI

struct ContentView: View {
    @State private var selection: String?
    var body: some View {
        NavigationSplitView {
            SidebarView(selection: $selection)
        } detail: {
            DetailView(selection: $selection)
        }
    }
}

struct SidebarView: View {
    @Binding var selection: String?
    let people = ["Finn", "Leia", "Luke", "Rey"]
    var body: some View {
        List(people, id: \.self, selection: $selection) { person in
            Text(person)
        }
        Text("selection = \(String(describing: selection))")
    }
}

struct DetailView: View {
    @Binding var selection: String?
    var body: some View {
        Text("selectedItem = \(String(describing: selection))")
    }
}
//------------------------

#Preview {
    ContentView()
}

   

OK. Now I know "why" it is cleared when returning from the detail view.

It is to reset things ready for the next selection.

I worked out a way to keep the previously selected item selected, however that then screwed up the ability to select it again, and at one point, it screwed up all future selections.

I am marking this as resolved because my orginal question of "Why is the selection being reset back to nil on the iPhone when returning back to the Sidebar view" has been determined.

What I was attempting to do was to replicate (to some degree) the experience on the iPad to make the experiences across devices consisten (to some degree). The prior selected entry is kept on the iPad even when hiding and reshowing the sidebar. I was trying to do the same thing with the iPhone experience.

As a secondary point though, by using another @State variable and using the onTapGuesture() and onAppear() methods I found the balance I was looking for.

   

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.