【问题标题】:How to call on a view when a Picker selection is selected选择选取器选择时如何调用视图
【发布时间】:2021-11-11 20:28:29
【问题描述】:

I have a Picker and when one of the selections is selected I'm trying to make it call on my camera view, but nothing happens.我试图调用一个函数来让它打印,但这也不起作用。这是代码

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 移动到不同的位置,还尝试将文本放在按钮中,但没有成功,我们将不胜感激任何帮助或建议

【问题讨论】:

    标签: swiftui picker


    【解决方案1】:

    这是解决您问题的示例:

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

    【讨论】:

    • 这仍然可以在视图中显示相机而不是打印选择吗?如果是这样,如果我像这样设置struct deviceResults: Codable { let result:[Result] } struct Result: Codable, Hashable { let Device: String },我将如何处理enum,因为我正在解码来自服务器的选择项
    • @LloydEnglish:你是这个网站的新人,让我们解释一下,我们不是在这里把问题或问题混在一起!此外,一旦第一个问题得到解决,我们不会成为人质的答案和讨价还价解决另一个问题!您的问题是关于 Picker 的用例和检测选择!对于您的问题,我的回答是完整而简单的,对于其他问题提出另一个问题并简单地接受这个答案。 PS:您也可以使用数组代替 Enum!没问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多