0

I have a ToolbarItem that when tapped, brings a menu of either Fahrenheit or Celsius. Right now I have that if the user taps on "Celsius" the temperatures will change to celsius unit and vice versa. However, I would like to add a check mark when the user selects Fahrenheit or Celsius and have it change when they select it. Here is my code. I've tried utilizing Picker but not sure how to add the action or a button within it.

 .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Menu {
                        Button {
                            isCelsius = false
                        } label: {
                            Text("Fahrenheit °F")
                        }
                        Button {
                            isCelsius = true
                        } label: {
                            Text("Celsius °C")
                        }

                    } label: {
                        Image(systemName: "list.dash")
                    }
                    

                    
                }//toolbar item
            }//toolbar
Kevin
  • 27
  • 7

1 Answers1

0

You can put a Picker inside the Menu:
The .tag()s connect the selection to the state var.

            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Menu {
                        Picker("", selection: $isCelsius) {
                            Text("Fahrenheit °F").tag(false)
                            Text("Celsius °C").tag(true)
                        }
                    } label: {
                        Image(systemName: "list.dash")
                    }
                }
            }
ChrisR
  • 9,523
  • 1
  • 8
  • 26
  • Thanks! I tried a Picker but was using an index for selection and was unaware you could tie it to a Bool – Kevin Mar 29 '22 at 23:25