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

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 Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.