TabbedView Binding

I am trying to do a basic TabbedView. The code below runs and when you tap each tab it updates to show the current selected page.

The problem is when I uncomment the Binding in AView. That triggers an error in ContentView:


$tabIndex is marked "ContentView.swift:14:31: Cannot convert value of type 'Binding<Int>' to expected argument type 'Binding<_>'"


Until I uncomment the Binding the TabbedView is perfectly happy to accept a Binding<Int> but once I want to use that binding problem!


Any ideas? Am I doing this all wrong?


struct ContentView : View {
    @State var tabIndex: Int = 1
    var body: some View {
        TabbedView(selection: $tabIndex){
            AView().tabItemLabel(
                Text("Page A: \($tabIndex.value)")
            ).tag(0)
            AView().tabItemLabel(
                Text("Page B: \($tabIndex.value)")
                ).tag(1)
            AView().tabItemLabel(
                Text("Page C: \($tabIndex.value)")
                ).tag(2)
        }
        
    }
}


struct AView : View {
//    @Binding private var tabIndex: Int
    var body: some View {
        Text("Hello World ")
    }
}

Replies

Having same issue on brand new xcode install on catalina w/ new tab view project from template


After fixing the deprecations on tabItemLabel


Unfortunately, "Cannot convert value of type 'Binding<Int>' to expected argument type 'Binding<_>'" is Xcode's way of saying that something in the TabbedView.init's trailing closure is wrong. That doesn't narrow it down much.


For beta 3 (specifically for brianjs57's issue), after updating the default Tabbed Application project template code from the deprecated

.tabItemLabel(Image("first"))

to

.tabItem(Image("first"))

you'll get that error at the TabbedView init line ("Cannot convert value of type 'Binding<Int>' to expected argument type 'Binding<_>'") because the Quick Fix doesn't work correctly. The tabItem lines should be should be

.tabItem { Image("first") }

There is a full working example for Xcode Beta 3 by Alex Melnychuck here: https://gist.github.com/amelnychuck/ad488f5bbb4605d8f9c402a297c0f390