0

I am trying to have user click on image and take them to a different view where it has same image and text from array on that view and for each time they click on image in scollview the view pops up with that information. It will take data from array and show it on next view with the correct item from each toggle.

struct practice: View {

    @State var show = false
    var place = Placeinfo

    var body: some View {

        VStack {
            ScrollView(.horizontal) {
                ForEach(place) { item in
                    HStack(spacing: 30) {
                        VStack {
                            HStack {
                                VStack {
                                    Text(item.title).font(.headline)
                                    Text(item.subtitle).font(.caption)
                                    Button(action:{
                                        self.show.toggle()
                                    }) {
                                        Image(item.image).renderingMode(.original)
                                    }.sheet(isPresented: self.$show) { Detail() }
                                }
                            }

                        }.padding(.leading)
                    }
                }
            }
        }
    }
}


struct places : Identifiable{
    var id = UUID()
    var title: String = ""
    var subtitle: String = ""
    var image: String = ""
}


let Placeinfo = [
    places(title: "beach", subtitle: "Cozumel",
           image: "beach1"),
    places(title: "beach2", subtitle: "Caribbean", image: "beach2"),
    places(title: "beach3", subtitle: "CostaRica", image: "beach3")
]
Jorge
  • 2,530
  • 1
  • 19
  • 28
Robert
  • 9
  • 2

1 Answers1

0

You can add a places variable in your Detail view and pass your place from ForEach to your destination ex: Detail(place: place) In your Detail view bind your variable to relevant views

Mac3n
  • 4,189
  • 3
  • 16
  • 29
  • import SwiftUI struct Detail: View { @Binding var places: places // var place: places = Placeinfo[0] var body: some View { VStack{ Text(places.image) Image(places.title) } } } struct Detail_Previews: PreviewProvider { static var previews: some View { Detail(places: places) } } Im still getting error. I passed in my place variable from the foreach into my detail view. – Robert Feb 08 '20 at 17:11
  • There is no need to define as @Binding object, just define as a variable. Just `var place: places` and pass it in your Detail(place: place)` – Mac3n Feb 08 '20 at 17:13
  • the only error im getting is in my ContentView when im calling the Detail function. 'Detail.Type' is not convertible to '(places, places) -> Detail' . .sheet(isPresented: self.$show) { Detail(places: place) } – Robert Feb 08 '20 at 17:23
  • If you mean in your preview section, you should pass the first element of your PlaceInfo as a parameter in your preview, then it should pass the error – Mac3n Feb 08 '20 at 17:25
  • is there a way i can just use a list and navigation link without making it have the list format – Robert Feb 08 '20 at 17:32