Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

400
Views
How Can I Pass Function Through A Toggle Swiftui

State var:

@State var toggleIsOn = false

Toggle:

Toggle(isOn: $toggleIsOn, label: {Text("Notifications")})

I want the following buttons, to represent each state of the toggle, on and off:

                // Will request to send notifications, if success; will schedule them.
                Button("request") {
                    NotificationManager.instance.requestAuthorization()
                }
                // Will clear the queue of notifications, and delete any delivered.
                Button("cancel") {
                    NotificationManager.instance.cancelNotifications()
                }

I found the functions in this video: https://www.youtube.com/watch?v=mG9BVAs8AIo

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

use

onChange(of:)

for example:

    struct ContentView: View {
    @State var toggleIsOn: Bool = false

    var body: some View {
        Toggle(isOn: $toggleIsOn, label: {Text("Notifications")})
        .onChange(of: toggleIsOn) { isOn in
             if isOn {
                  NotificationManager.instance.requestAuthorization()
             } else {
                  NotificationManager.instance.cancelNotifications()
             }
        }
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!