我试图在选择菜单项时打上复选标记。我尝试给出 @State var selectedSort : Int = .zero 并给出 id(selectedSort) 但它不起作用。我怎么解决这个问题 ?
这是我的代码;
struct SortMenuPickerView : View {
@Binding var sortClicked : Bool
@ObservedObject var productListViewModel = ProductListViewModel()
@State var sortListArray : [ProductListSortAndFilterList]
var function: () -> Void
@Binding var sortId : String
var body : some View {
HStack(alignment: .center){
Spacer()
Menu {
ForEach(sortListArray,id:\.id){ item in
if item.id == "sort" {
ForEach(item.sortList ?? [],id:\.id) { data in
Button(action: {
sortId = (data.id ?? "")
self.function()
print("selected item is : \(data.id!)")
}) {
Text(data.name ?? "")
.tag(data.id)
}
}
}
}
} label: {
SortView()
}
Run Code Online (Sandbox Code Playgroud)
尝试使用选择器样式菜单并可以根据您的要求进行定制...
其示例示例:
@State private var sort: Int = 0
var body: some View {
NavigationView {
Text("Hello World!")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Menu {
Picker(selection: $sort, label:Text("Sortingoptions")) {
Text("Sort1").tag(0)
Text("Sort2").tag(1)
Text("Sort3").tag(2)
}
}
label: {
Text("Sort")
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2294 次 |
| 最近记录: |