Answers for "swift ui get state"

1

how to pass state variable in swiftui

// View1:

struct FirstView: View {
    @State var count = 0
    var body: some View {
        VStack{
            Text("\(self.count)")
            Button(action:
                {self.count = self.count-10})
            {
                Text("-")
            }
            SecondView(count: self.$count)
        }
    }
}

// And View2:

struct SecondView: View {
    @Binding var count: Int
    var body: some View {
        Button(action: {self.count = 0}) {
            Text("Reset")
        }
    }
}
Posted by: Guest on March-13-2020

Code answers related to "Swift"

Browse Popular Code Answers by Language