0

I am trying to create two Pickers, one dependent on the other. Both come from two SwiftData models Ruta and Parada.

But I don't manage to paint the second one, if I try to binding the variable Parada, I get an error.



import SwiftUI
import SwiftData

struct FormFavouritesTestView: View {
    
    @State var loadData: ReadDataModel?
    @State var idRutaNuevo:String = "1"
    
    @Environment(\.modelContext) private var modelContext
    @Query private var paradas: [Parada]
    @State private var selectedParada: Parada?
    
    @Query var queryRutas:[Ruta]
    
    @Query(sort: \Ruta.idRuta) private var rutas: [Ruta]
    @State private var selectedRuta: Ruta?

    
    private func cargar(){
        loadData = ReadDataModel(modelContext: modelContext)
    }

    
    var body: some View {
        NavigationView {
            Form {
                
                Section(
                    header: Text("Dirección y parada")
                ){
                    
                    Picker(selection: $selectedRuta, label: Text("Dirección")) {
                        Text("Selecciona")
                        ForEach(rutas) { ruta in
                            Text(ruta.idRuta + " - " + ruta.name)
                                .tag(Optional(ruta))
                        }
                    }.onChange(of: selectedRuta){
                        print(selectedRuta!.idRuta)
                        if selectedRuta!.idRuta == "1" {
                            idRutaNuevo = "2"
                        }else{
                            idRutaNuevo = "1"
                        }
                        
                    }
                  
                    PickerParadasView(selectedParada: $selectedParada, idRutaNuevo: $idRutaNuevo)
                    
                    
                    Button("Done") {
                        print("La  dirección \(idRutaNuevo)")
                    }
                    
                }
                
                
            }
            .navigationTitle("Nueva Ruta")

        }
        .onAppear(){
            
            cargar()
            
            selectedRuta = queryRutas.first
            if let rutaAux = selectedRuta {
                idRutaNuevo = rutaAux.idRuta
                print("Entro aquí \(rutaAux.idRuta)")
            }
            
        }
        
    }
}

struct PickerParadasView:View {
    
    @Binding private var paradas: [Parada]
    
    @Binding var selectedParada: Parada?
    @Binding var idRutaNuevo:String
    

    
    init(){
        
        let predicate = #Predicate<Parada> {
            $0.idRuta == "2"
        }
        $paradas = Query(filter: predicate, sort: \.id)
         
    }
    
    
    var body: some View{
        
        Picker(selection: $selectedParada, label: Text("Parada")) {
                ForEach(paradas) { parada in
                    Text("\(parada.id) - " + parada.title)
                        .tag(Optional(parada))
                }
        }
    }
}

#Preview {
    FormFavouritesTestView()
        .modelContainer(for: [Parada.self, Ruta.self, Favorito.self], inMemory: true)
}


I need Parada to be filtered by [RUTA].idRoute and sorted by Parada.id.

Someone can help me?

Thank you in advanced!

2
  • 1
    Instead of onChange try a computed binding for the picker Commented Jun 29, 2024 at 18:34
  • Note NavigationView is deprecated, use NavigationStack instead. Commented Jun 29, 2024 at 22:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.