Trigger an action when toggle
Liang Wang
Posted on February 11, 2024
I have a toggle in my app Settings where I can switch on and off the onboarding view.
Previously I was using if/else at App start to check if show onboarding, but now I have app state as Environment Object, I need to be able to change app state with something like:
if isOnboarding {
appManager.restartOnboarding()
}
So I found this link: and the example code like below
struct ContentView: View {
@State private var isDisplayed = false
var body: some View {
Toggle("", isOn: $isDisplayed)
.onChange(of: isDisplayed) {
print("Action")
}
.onChange(of: isDisplayed) { oldValue, newValue in
// action...
print(oldValue, newValue)
}
}
}
This works for me.
💖 💪 🙅 🚩
Liang Wang
Posted on February 11, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
githubcopilot AI Innovations at Microsoft Ignite 2024 What You Need to Know (Part 2)
November 29, 2024