0
votes

I've created a simple swiftui view with a simple vstack with a text and a toggle. I can't understand how to trigger toggle's changes because the method I've developed is not working.

This is the code

struct SquadMemberDetailView: View {
    
    @State var admin = false

    var body: some View {

       VStack(alignment: .leading) {

           Text("Amministratore")
                            .font(.body)
                            .foregroundColor(Color("DarkGray"))
                            .padding([.top, .leading, .trailing])
                        
           Toggle(isOn: $admin){
           }
           .onTapGesture(perform: {
               self.admin = !self.admin
                manageToggle()
            })
            .labelsHidden()
            .padding(.horizontal)

       }

    }

}

The onTapGesture method is never called when I switch the toggle state.

Any idea?

1

1 Answers

0
votes

Toggle changes bound variable by itself you should not do it manually, so I believe you wanted something like

SwiftUI 2.0+:

Toggle("", isOn: $admin)
   .onChange(of: admin) { _ in
        manageToggle()
   }
   .labelsHidden()
   .padding(.horizontal)

SwiftUI 1.0:

import Combine

...

    Toggle("", isOn: $admin)
       .onReceive(Just(admin)) { _ in
            manageToggle()
       }