Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

swift - MenuPicker Sort when selected SwiftUI

Im trying to select one of sort in menu and sending backend . But somehow onTapGesture is not triggered. I also tried to use button but it didn't work either. Or is there any other way to trigger any of sort In menu picker.

var sortMenuPicker : some View {
    
    HStack(alignment: .center){
        
        Spacer()
        
        Picker(selection: $selection, label: SortView().frame(height:UIScreen.main.bounds.height * 0.050), content: {
            ForEach(sortListArray, id: .uid, content: { item in
                ForEach(item.sortList ?? [],id:.uid) { data in
                    
                    if item.id == "sort" {
                        
                        Text(data.name ?? "")
                            .onTapGesture {
                                
                                sortId = data.id
                                
                                getProductList()
                                
                                
                      }
                            
                    }
                    
                }
                
            })
        })
        .pickerStyle(MenuPickerStyle())
       
        
      
    

enter image description here

question from:https://stackoverflow.com/questions/66063419/menupicker-sort-when-selected-swiftui

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add a tag to your Text() and remove Tap Gesture.

Text(data.name ?? "").tag(data.uid) //<-- Here

And use onReceive to listen to your selection

.onReceive(Just(selection)) { (_) in
    print(selection)
}

Note: for this you need to import Combine


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...