Skip to content

Instantly share code, notes, and snippets.

@lucianoschillagi
Created November 10, 2024 22:00
Show Gist options
  • Save lucianoschillagi/3186b4746b901d748ffb7fc5ab843fa4 to your computer and use it in GitHub Desktop.
Save lucianoschillagi/3186b4746b901d748ffb7fc5ab843fa4 to your computer and use it in GitHub Desktop.
SwiftUI View Lifecycle Methods → onChange
import SwiftUI
struct ContentView: View {
// tracking changes of this property via 'onChange' method
@State private var notificationsEnabled = false
var body: some View {
Toggle("Notifications", isOn: $notificationsEnabled)
.padding()
.font(.title)
.onChange(of: notificationsEnabled) { newValue in
print(newValue ? "Notifications Enabled" : "Notifications Disabled")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment