Created
November 10, 2024 22:00
-
-
Save lucianoschillagi/3186b4746b901d748ffb7fc5ab843fa4 to your computer and use it in GitHub Desktop.
SwiftUI View Lifecycle Methods → onChange
This file contains 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
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