TabItem Not Respected In TabView

Hello, I'm a new programmer here, so this may be an error on my part, however I have tried my best to research the issue and believe I may have discover a bug. I have a set of tabItems inside of a TabView using a variable to track the selected tab called selectedIndex. I have added in a text view to watch the selected tab. This works correctly in the canvas and in a simulator, however whenever I build this on an iOS Device or for my Mac the selectedIndex does not change when selecting tabs like it does in the canvas and simulator. Instead it just stays at the default 0. Any assistance would be great. :-)

import SwiftUI

struct ContentView: View {
    @State private var selectedIndex = 0
    
    var body: some View {
        Text("\(selectedIndex)")
        TabView(selection: $selectedIndex) {
            FilteredApplicantListView()
                .tabItem { Label("Applicant Processor", systemImage: "person.3.sequence.fill") }
                .tag(0)
            QuickFinancials()
                .tabItem { Label("Quick Financials", systemImage: "dollarsign.gauge.chart.leftthird.topthird.rightthird") }
                .tag(1)
            MoveInView()
                .tabItem { Label("Move Ins", systemImage: "figure.walk.arrival") }
                .tag(2)
            MoveOutView()
                .tabItem { Label("Move Outs", systemImage: "figure.walk.departure") }
                .tag(3)
        }
    }
}

I tested on a Mac macOS Sonoma 14.5, Xcode 16.0ß2.

I created dummy Views to call, but the does not matter.

struct FilteredApplicantListView: View {
    var body: some View {
        Text("FilteredApplicantListView")
    }
}

struct QuickFinancials: View {
    var body: some View {
        Text("QuickFinancials")
    }
}

struct MoveInView: View {
    var body: some View {
        Text("MoveInView")
    }
}

struct MoveOutView: View {
    var body: some View {
        Text("MoveOutView")
    }
}

It works, as shown below:

What is your configuration ?

Hi @erfinb, I see that @Claude31 tested this on Mac, and I tested it on iOS and get the same results as them.

Please let us know what Xcode version you're on and what iOS version you're targeting.

Try creating a new project that contains only this code and see if you get the same behavior.

import SwiftUI
struct ContentView: View {
    @State private var selectedIndex = 0
    
    var body: some View {
        Text("\(selectedIndex)")
        TabView(selection: $selectedIndex) {
            Text("1")
                .tabItem { Label("Applicant Processor", systemImage: "person.3.sequence.fill") }
                .tag(0)
            Text("2")
                .tabItem { Label("Quick Financials", systemImage: "dollarsign.gauge.chart.leftthird.topthird.rightthird") }
                .tag(1)
            Text("3")
                .tabItem { Label("Move Ins", systemImage: "figure.walk.arrival") }
                .tag(2)
            Text("4")
                .tabItem { Label("Move Outs", systemImage: "figure.walk.departure") }
                .tag(3)
        }
    }
}

The placeholder Text instead of your views can help simplify to debug.

Hello  @Claude31 and @Vision Pro Engineer , thank you both for your responses. Things have been busy, and unfortunately, I wasn't able to respond. I have made these changes to both my beta app as well as tested it with the sample code provided. It appears to run fine in a simulator, but then whenever I run it on my Mac or run it on an iOS device, it seems to not be tracking the selected item. The screenshots from the Mac window are showing the issue when running in a real window, and the iOS screenshots are from the simulator. This will result in having an issue where if I click on another window, such as Safari, then the test app will return to the first tab. I am using SwiftData in its default state, but since it's not tracking anything in the sample app, I don't think that is the issue. Any ideas on what I could be doing wrong? (This is being run with the latest Xcode Beta for iOS 18, and has been this way since I originally wrote my post.) Thank you all again for any help you can offer, and I really appreciate your responses. :-)

@State private var selectedIndex = 0

    var body: some View {
        Text("\(selectedIndex)")
        TabView(selection: $selectedIndex) {
            View1()
                .tabItem {Label("Tab 1", systemImage: "dollarsign.gauge.chart.leftthird.topthird.rightthird")}
                .tag(0)
            View2()
                .tabItem {Label("Tab 2", systemImage: "dollarsign.gauge.chart.leftthird.topthird.rightthird")}
                .tag(1)
        }
    }
}

#Preview {
    ContentView()
        .modelContainer(for: Item.self, inMemory: true)
}

hey @erfinb , I unfortunately still can't reproduce your issue. I ran it on my Mac and had no issues. What version of macOS are you on?

Can you try creating a new project and pasting the code in there? Then we'll know if it's your project or the code you have.

Hello @Vision Pro Engineer I created a GitHub project for it which I believe is setup the way we have discussed. You can see the error if you run it natively on your iOS or iPadOS device, or if you run for the Mac on an M1 device. It cannot be in the Simulator. You will see that in the simulator it functions as expected, however whenever run on a device it does not track the selected tab. https://github.com/erfind/TestTabs

@erfinb thank you so much for updating that project, I now see that this is only when run on Mac (designed for iPad), whereas I was testing with just Mac.

Can you please file a bug report at https://feedbackassistant.apple.com under "Developer Technologies and SDKs" with the platform being iOS and the technology being SwiftUI?

Then paste the FB number here. Thanks so much for catching that and bringing it to attention!

Sydney

TabItem Not Respected In TabView
 
 
Q