0

I am learning to use Swiftui and I need your help. The following pickers are present in my project:

    Picker(selection: $selectedChoose, label: Text("Cosa ti serve?")) {
            ForEach(0 ..< choose.count) {
            Text(self.choose[$0]).tag($0)
    }
    }.pickerStyle(SegmentedPickerStyle())
            //WheelPickerStyle



    Picker(selection: $selectedCountry, label: Text("Città")) {
            ForEach(0 ..< country.count) {
            Text(self.country[$0]).tag($0)
    }
    }

    Picker(selection: $selectedAppartamento, label: Text("Tipo appartamento")) {
            ForEach(0 ..< appartamento.count) {
            Text(self.appartamento[$0]).tag($0)
    }
    }

    Picker(selection: $selectedCamera, label: Text("Tipo camera")) {
            ForEach(0 ..< camera.count) {
            Text(self.camera[$0]).tag($0)
    }
    }

At the end of the View, I have the following Text to which I will add the NavigationLink:

Text("Mostra annunci")
.font(Font.custom("Helvetica Neue", size: 15))
.foregroundColor(.white)
.frame(width: 150, height: 30).foregroundColor(Color.white)
.background(Color(red: 0.649, green: 0.18, blue: 0.117)).cornerRadius(10).padding(15)

Is it possible to link user choices with different views? I mean, if a user selects a different city, room type and type of apartments, how can I link these choices to different views? Each view should show results based on the different choices made by the user in the pickers above. How can I do that?

Thank you in advance :)

  • I think you need to render the views conditionally (if-else statements) or use NavigationView with NavigationLinks. As to how to pass the data, you will have to create custom views and pass data into them as parameters, and if you modify data from these views you pass a binding to those variables instead. Quite useful thing to have is an EnvironmentObject, you might like to look at that. – Antoni Silvestrovič Apr 10 '20 at 22:46

1 Answers1

0

you can popup new views using the .sheet(...). The restriction is that you can only have one of those. So you would have to do something like this:

        .sheet(isPresented: $showSheet) {
            if self.selectedCountry == "myCountry" {
                Text("myCountry")
            }
            else if self.selectedCountry == "yourCountry" {
                Text("yourCountry")
            }
            ... lots of other if else

In your case I would reconsider the whole structure. Use just one sheet option and pass in the choices the user made. And inside the new view present the data as chosen.