davede3103
davede3103

Reputation: 117

How to display the selected menu item in SwiftUI?

I want to selected menu to get displayed and saved when the user comes back later. my current code just displays the selected item, but not getting saved when I close the sheet and comes back.


  @State var selectedAge: Int = .zero
  
  var body: some View {
    Menu {
      ForEach(myViewModel.MyModel.selectAge.indices, id: \.self) { indice in
        Button(action: {
          selectedAge = indice
        }) {
          if selectedAge == indice {
            Text("\(myViewModel.MyModel.selectAge[indice])")
          }
          else {
            Text("")
          }
        }
      }
    } label: {
      Text("\(myViewModel.MyModel.selectAge[selectedAge])")
    }
  }

This code from my Model

var selectedAge: [String] = ["12", "15", "18", "21", "24"]

Please guide me to solve this issue.

Upvotes: 4

Views: 5999

Answers (3)

Kaunteya
Kaunteya

Reputation: 3090

You can conditionally add a Button or a Toggle to the Menu

Menu {
    ForEach(data, id: \.self) { indice in
        if selectedAge == indice {
            // Shows a menu item with a tick
            Toggle("Index \(indice)", isOn: .constant(true))
        }
        else {
            // Shows a menu item without a tick
            Button("Index \(indice)") {
                selectedAge = indice
            }
        }
    }
} label: {
    Text("")
}

Upvotes: 1

davede3103
davede3103

Reputation: 117

I used this code to solve my issue.

Menu {
    ForEach(myViewModel.myModel.selectAge, id: \.self) { index in
        Button {
            myViewModel.myModel.selectAge = "\(index)"
        } {
            if myViewModel.myModel.selectedAge == index {
                Label("\(index)", systemImage: "checkmark")
            } else {
                Text("\(index)")
            }
        }
    }
} label: {
    Text("\(myViewModel.myModel.selectedAge)")
}

and this is insert in my model

var selectedAge = "12"
var selectedAge: [String] = ["12", "15", "18", "21", "24"]

Upvotes: 2

Rahul Mayani
Rahul Mayani

Reputation: 3831

Try this:

// selectedAge1 array list replace with your data model list...

import Combine

class MenuAgeSettings: ObservableObject {
    @Published var selectedAge: String {
        didSet {
            UserDefaults.standard.set(selectedAge, forKey: "selectedAge")
        }
    }
    
    init() {
        self.selectedAge = UserDefaults.standard.object(forKey: "selectedAge") as? String ?? ""
    }
}

struct MenuView: View {
    var selectedAge1: [String] = ["12", "15", "18", "21","24"]
    
    @ObservedObject var menuSettings = MenuAgeSettings()
        
    var body: some View {
       Menu{
          ForEach(self.selectedAge1.indices, id: \.self){ indice in
              let text = self.selectedAge1[indice]
               Button(action : {
                   menuSettings.selectedAge = text
               }) {
                if menuSettings.selectedAge == text {
                      Label(text, systemImage: "checkmark")
                   } else {
                      Text(text)
                   }
               }
           }
        } label: {
            if menuSettings.selectedAge.isEmpty {
                Text(selectedAge1[.zero])
            } else {
                Text(menuSettings.selectedAge)
            }
        }
    }
}

Upvotes: 0

Related Questions