How do I segue from one SwiftUI view to another, when the object i want to be clicked is a custom element in a carousel?

87 Views Asked by At

I have been using a SwiftUI package dependency to make a carousel effect in my app. what I want is for each of these custom carousel elements which consist of an image and some text, when clicked, to segue to the same view. It's worth mentioning my whole app is in swift, but because of this package dependency this part of it is in SwiftUI. So if somebody has a way to segue from a SwiftUI view to a swift view controller, that would be even better. In conclusion, I need some way to turn these individual elements into segues, that all lead to the same view, or a way to segue from a view to a view controller

So far I have tried to use a Navigation Link, however I don't think I'm using it correctly since I am new to SwiftUI. I've also tried to use .segueForUnwinding, but that seems to not be in SwiftUI anymore.

The segue code should fall somewhere in the ZStack

here is the code:

import SwiftUI
import SwiftUIInfiniteCarousel

struct Element {
    let imageName: String
    let text: String
    let recipe: String
}

let elements: [Element] = [
    Element(imageName: "logo", text: "logo 1", recipe: "This is a test recipe. #1"),
    Element(imageName: "Test", text: "logo 2", recipe: "This is a test recipe. #2"),
    Element(imageName: "logo", text: "logo 3", recipe: "This is a test recipe. #3"),
    Element(imageName: "logo", text: "logo 4", recipe: "This is a test recipe. #4")
]

struct FavoritesSwiftUIView: View {
    var body: some View {
        
        ZStack {
            VStack {
                Text("Fusio Favorites")
                    .font(Font.custom("Marker Felt", size: 48))
                    .fontWeight(.bold)
                    .padding(.top, 10)
                    .padding(.bottom, 100)

                
                InfiniteCarousel(data: elements, height: 300, cornerRadius: 15, transition: .scale) { element in
                    ZStack(alignment: .bottomLeading) {
                        Image(element.imageName)
                            .resizable()
                            .scaledToFit()
                            .frame(height: 300)
                            .overlay(
                                RoundedRectangle(cornerRadius: 15)
                                    .fill(Color.black.opacity(0.3))
                            )
                            .alignmentGuide(.bottom) { d in
                                d[.bottom]
                            }
                            

                        Text(element.text)
                            .font(.subheadline)
                            .foregroundColor(.white)
                            .padding()
                            .alignmentGuide(.bottom) { d in
                                d[.bottom]
                            }
                            
                    }
                }
            }
        }
    }
}

The name of the SwiftUI view I need to segue to is 'FavoritesResultSwiftUIView', and the name of the view controller is 'FavoritesResultViewController'

The name of the code file above is FavoritesSwiftUIView, and is hosted by FavoritesViewController

0

There are 0 best solutions below