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

@Binding variable as a paramater

Forums > SwiftUI

Hi I have the example below, I get an error on the PatientDetails_Previews Struct as it needs a value for the variable I declared as "@Binding var currentTab" and Im confused when I pass an integer like Info(currentTab: 1) I get an error ?

Kindest Regards

struct Details: View {
    @State var currentTab: Int = 0

    var body: some View {
        Text(currentTab)
        }
}

struct Info: View {
    @Binding var currentTab: Int

    var body: some View {
       Text(currentTab)
    }
}

struct PatientDetails_Previews: PreviewProvider {
    static var previews: some View {
        Info()
    }
}

1      

Because you are passing an Int when a Binding<Int> is expected.

In a preview, you can use .constant() to avoid having to set up a binding. In this case it would be:

Info(currentTab: .constant(1))

1      

Im trying to separate the views in project below TabBarView and TabBarItem in separate files and the developer used Namespace.ID for namspace so yousing your mothed wont accomplish the desired animaton

https://www.youtube.com/watch?v=RPCTAv_e2kA

Kindest Regards

1      

I don't understand what Namespace.ID has to do with passing an Int into a View as a Binding, especially since that Int is just being displayed in a Text element. Namespace.ID is not an Int.

1      

I really dont know but I tried your solution and animation was different

1      

What animation? There is no animation in the sample code you posted.

You asked about how to pass something to a Binding parameter in a preview. What I told you is how you can do that. If your question is actually about something else, ask that.

1      

If code is as below how to intialise the Namespace ? still getting the missing argument error

struct Details: View {
    @State var currentTab: Int = 0

    var body: some View {
        Text(currentTab)
        }
}

struct Info: View {
    @Binding var currentTab: Int
     let namespace: Namespace.ID

    var body: some View {
       Text(currentTab)
    }
}

struct PatientDetails_Previews: PreviewProvider {
    static var previews: some View {
        Info()
    }
}

1      

Like so:

struct PatientDetails_Previews: PreviewProvider {
    @Namespace static var namespace
    static var previews: some View {
        Info(currentTab: .constant(1), namespace: namespace)
    }
}

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.