Created
February 12, 2020 11:06
-
-
Save jhowlin/f88ebdd1c6864d1144cca33b0abddef0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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