Skip to content

Instantly share code, notes, and snippets.

@jhowlin
Created February 12, 2020 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhowlin/f88ebdd1c6864d1144cca33b0abddef0 to your computer and use it in GitHub Desktop.
Save jhowlin/f88ebdd1c6864d1144cca33b0abddef0 to your computer and use it in GitHub Desktop.
class CounterController: ObservableObject {
static let shared = CounterController()
var timer:Timer?
@Published var numTimes = 0
init() {
timer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true) { [weak self] timer in
self?.numTimes += 1
}
}
}
struct CounterView: View {
@ObservedObject var controller = CounterController.shared
var body: some View {
return Group {
if controller.numTimes > 0 {
LabelView(numTimes: controller.numTimes)
}
Button(action: {
self.controller.numTimes += 1
}) { Text("Tap me")}
}
}
}
struct LabelView: View {
@State var numTimes = 0
var body: some View {
return Text("You've tapped \(numTimes) times")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment