选择选择器选择时如何调用视图

How to call on a view when a Picker selection is selected

我有一个 Picker,当其中一个选项被选中时,我试图让它在我的相机视图中调用,但没有任何反应。我试图调用一个函数来打印它,但也没有用。这是代码

Picker(selection: $selection, label: Text("Discover Plug")) {
    ForEach(vm.dataSet, id:\.self) { item in
        Text(item.Device).tag(item.Device)
    }.onTapGesture{
       //CameraView()
       //print("selected")
         Selected()
    }    
}

func Selected() {
        print("selected")
}

我尝试将 .onTapGesture 移动到不同的地方,也尝试将文本放在按钮中,但没有用,如有任何帮助或建议,我们将不胜感激

这是解决您的问题的示例:

enum CameraEnum: String, CaseIterable { case telephoto, wide, ultraWide}

struct ContentView: View {
    
    @State private var selection: CameraEnum = .wide
    
    var body: some View {
        
        Picker(selection: $selection, label: EmptyView(), content: {

            ForEach(CameraEnum.allCases, id:\.self) { item in
                
                Text(item.rawValue)
                
            }
 
        })
        .onAppear() { cameraFunction(selection) }
        .onChange(of: selection, perform: { value in cameraFunction(value) })
        
    }
    
    private func cameraFunction(_ cameraValue: CameraEnum) {
        print("Your camera selection is:", cameraValue)
    }
    
}