【问题标题】:How to display the selected menu item in SwiftUI?如何在 SwiftUI 中显示选定的菜单项?
【发布时间】:2021-02-12 10:57:04
【问题描述】:

我想在用户稍后回来时显示并保存所选菜单。我当前的代码只显示选定的项目,但当我关闭工作表并返回时没有保存。


  @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])")
    }
  }

此代码来自我的模型

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

请指导我解决这个问题。

【问题讨论】:

  • 基本上可以将selectedAge存储在UserDefaults中。
  • 是的,我已经实现了用户默认设置,并且它适用于其他步进器、选择器小部件。但它不仅适用于菜单。
  • 能否请您使用 UserDefaults 版本编辑您的问题,我们可以看到您做错了什么,因为您需要的只是持久性。
  • 我认为,由于@state,当用户关闭并再次打开时,它被替换为 .zero (第一个索引 [“12”])。会是这个原因吗?
  • 没错,这就是我要说的......您没有保留任何 selectedAge,因此当您打开该菜单时,您会看到 selectedAge 数组的第一项。你也可以使用@AppStorage

标签: ios arrays swift swiftui swiftui-list


【解决方案1】:

我使用此代码解决了我的问题。感谢其他花时间帮助我的开发者!

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

这是插入到我的模型中

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

【讨论】:

    【解决方案2】:

    试试这个:

    // selectedAge1 数组列表替换为您的数据模型列表...

    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)
                }
            }
        }
    }
    

    【讨论】:

    • 它正在被检查,但是当我再次关闭并打开屏幕时,它被重置为数组的第一个索引
    猜你喜欢
    • 1970-01-01
    • 2015-10-26
    • 2020-05-21
    • 1970-01-01
    • 2012-12-16
    • 2019-02-06
    • 2021-05-18
    • 2014-06-10
    • 2020-06-08
    相关资源
    最近更新 更多