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

TabView incorrectly changing selected tab

Forums > SwiftUI

I'm using TabView's selection attribute in combination with @SceneStorage as shown in the portfolio app tutorial.

import SwiftUI

struct ContentView: View {

  @SceneStorage("selectedView") var selectedView: String?

  var body: some View {
    TabView(selection: $selectedView) {
      KindredList()
        .tag(KindredList.tag)
        .tabItem {
          Image(systemName: "person.3")
          Text("Characters")
        }
      ReferenceView()
        .tag(ReferenceView.tag)
        .tabItem {
          Image(systemName: "books.vertical")
          Text("Reference")
        }
    }
  }

}

For some reason, if I switch over to the ReferenceView tab, my app just loves to stay on it. If I return to the home screen? Bam. ReferenceView. Tap the toolbar item I have in my KindredList? Bam. ReferenceView. The reference view will come up constantly until I've force-quit the app, and even then it sometimes comes back.

And before anyone asks: yes, KindredList.tag and ReferenceView.tag are, in fact, different.

Here is a short recording of the problem.

3      

Try @SceneStorage("selectedView") var selectedView: String = KindredList.tag. I could recreate it with Optional but not without. I think the nil value is the problem.

3      

That seems to have done it! Thanks.

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!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.