Skip to content

Instantly share code, notes, and snippets.

@shaundon
Created June 27, 2020 12:54
Show Gist options
  • Save shaundon/a0534776c33a20d7b200743801942f09 to your computer and use it in GitHub Desktop.
Save shaundon/a0534776c33a20d7b200743801942f09 to your computer and use it in GitHub Desktop.
import SwiftUI
class MyModel: ObservableObject {
@Published var segmentedControlValue: Int = 0 {
didSet {
self.accumulator += 1
print("segmentedControlValue set to \(self.segmentedControlValue)")
}
}
@Published var accumulator: Int = 0
}
struct TestView: View {
@ObservedObject var model = MyModel()
var body: some View {
VStack {
Text("Pressed \(model.accumulator) times")
Picker(selection: $model.segmentedControlValue, label: Text("Choose one")) {
Text("0").tag(0)
Text("1").tag(1)
}.pickerStyle(SegmentedPickerStyle())
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment