Button action inside tabItem is not executing

I have this very simple code below and I am using the currentMenu to set the current tab's proper icon. But neither the button action nor the onTapGesture are executing as I see no print messages at all. It seems taps inside of tabItem are not registering.

NavigationView { }
            .tabItem {
                Button {
                    print("chat tapped")
                    env.currentMenu = DeChatMenus.PrivateDM
                } label: {
                    Image(env.currentMenu == DeChatMenus.PrivateDM ? "Chat.Sel" : "Chat")
                }
            }
            .tag(DeChatMenus.PrivateDM)

I switched to using .task in SwifttUI 3.0 and that does respond. This feels wrong to me but whatever.

do you want to be notified of a tab change?

class Model: ObservableObject {
    static let singleton = Model()
    private init() {}
    
    var selection = 0 {
        didSet {
            print("selection changed to \(selection)")
        }
    }
}

struct ContentView1: View {
    @ObservedObject var model = Model.singleton
    
    var body: some View {
        TabView(selection: $model.selection) {
            Text("hello world 1").tabItem { Image(systemName: "photo") }.tag(1)
            Text("hello world 2").tabItem { Image(systemName: "camera") }.tag(2)
        }
    }
}

// --- or ----

struct ContentView2: View {
    static var currentTab = 0
    
    var selection = Binding<Int> {
        currentTab
    } set: {
        print("tab changed to \($0)")
        currentTab = $0
    }

    var body: some View {
        TabView(selection: selection) {
            Text("hello world 1").tabItem { Image(systemName: "photo") }.tag(1)
            Text("hello world 2").tabItem { Image(systemName: "camera") }.tag(2)
        }
    }
}
Terms of Service

Privacy Policy

Cookie Policy