ios – SwiftUI: Why is my view pushed and popped instantly when being known as from ListView

ios – SwiftUI: Why is my view pushed and popped instantly when being known as from ListView


I’ve not too long ago began working with iOS and SwiftUI. I ported a easy solitaire-type sport over from Android and I’ve it largely working, however now I am attempting to implement a view that renders earlier profitable arms. I am utilizing CoreData to retailer the profitable hand values, winner’s identify, rating and date. I’ve confirmed that the info is saved and fetched accurately from CoreData. I can construct a listview that reveals all of the profitable arms, and when any merchandise within the record is clicked, the profitable knowledge string ought to be handed to the WinningHandView to render the view, with an choice to share if the consumer so wishes. Every part works wonderful till I click on on one of many profitable scores, after which the app appears to be like prefer it’s attempting to load the brand new view, nevertheless it instantly returns the app all the way in which to the ContentView (house) display screen.

Right here is the Corridor Of Fame Listing View code that populates the record from CoreData. The CoreData question completes efficiently and populates the record:

import SwiftUI
import CoreData


struct HOFListView: View {
    
    @FetchRequest(sortDescriptors: [SortDescriptor(.date)])
    var winners: FetchedResults
    
    
    var physique: some View {
        ZStack{
            Shade.cardTableGreen.edgesIgnoringSafeArea(.all)
            VStack {
                Textual content("Excessive Scores")
                    .font(.title)
                    .foregroundColor(.white)
                    .daring(true)
                NavigationStack {
                    Listing(winners, id: .self) { winner in
                        NavigationLink(winner.identify ?? "none", worth: winner.winningData)
                        Textual content("Rating: (winner.rating)")
                        Textual content("Date: (winner.date ?? Date())")
                        
                    }
                    .scrollContentBackground(.hidden)
                    .navigationDestination(for: String.self, vacation spot: WinningHandView.init)
                } //NavigationStack
            }
        }
    }
}

#Preview {
    HOFListView()
}

Right here is the WinningHandView, which renders the winningData worth into the varied rows/columns of playing cards. I’ve verified that this worth is being handed and parsed accurately:

import SwiftUI

struct WinningHandView: View {
    
    let winningHandDetails: String
    
    var physique: some View {
        ZStack {
            Shade.cardTableGreen.edgesIgnoringSafeArea(.all)
            let theWinningHandDetails = winningHandDetails.parts(separatedBy: "|")
            
            VStack {
                Textual content("Your profitable hand:")
                    .font(.title)
                    .foregroundColor(.white)
                    .padding()
                    .daring(true)
                HStack {
                    VStack {
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                        }
                        else {
                            Picture(theWinningHandDetails[0])
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.prime, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[1])
                                .padding(.prime, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.prime, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[2])
                                .padding(.prime, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.prime, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[3])
                                .padding(.prime, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Textual content("Rating: ")
                                .foregroundColor(.white)
                                .daring(true)
                        }
                        else {
                            Textual content("Rating: (theWinningHandDetails[21])")
                                .foregroundColor(.white)
                                .daring(true)
                        }
                        
                    } //VStack

                    VStack {
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                        }
                        else {
                            Picture(theWinningHandDetails[4])
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.prime, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[5])
                                .padding(.prime, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.prime, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[6])
                                .padding(.prime, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.prime, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[7])
                                .padding(.prime, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Textual content("Rating: ")
                                .foregroundColor(.white)
                                .daring(true)
                        }
                        else {
                            Textual content("Rating: (theWinningHandDetails[22])")
                                .foregroundColor(.white)
                                .daring(true)
                        }
                        
                    } //VStack
                    VStack {
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                        }
                        else {
                            Picture(theWinningHandDetails[8])
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.prime, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[9])
                                .padding(.prime, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.prime, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[10])
                                .padding(.prime, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.prime, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[11])
                                .padding(.prime, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Textual content("Rating: ")
                                .foregroundColor(.white)
                                .daring(true)
                        }
                        else {
                            Textual content("Rating: (theWinningHandDetails[23])")
                                .foregroundColor(.white)
                                .daring(true)
                        }
                    } //VStack
                    VStack {
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                        }
                        else {
                            Picture(theWinningHandDetails[12])
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.prime, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[13])
                                .padding(.prime, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.prime, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[14])
                                .padding(.prime, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.prime, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[15])
                                .padding(.prime, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Textual content("Rating: ")
                                .foregroundColor(.white)
                                .daring(true)
                        }
                        else {
                            Textual content("Rating: (theWinningHandDetails[24])")
                                .foregroundColor(.white)
                                .daring(true)
                        }
                        
                    } //VStack
                } //HStack
                HStack {
                    VStack {
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding()
                        }
                        else {
                            Picture(theWinningHandDetails[20])
                                .padding()
                        }
                        if theWinningHandDetails.depend == 0 {
                            Textual content("Reduce card")
                                .foregroundColor(.white)
                                .daring(true)
                                .padding(.prime, -15.0)
                                
                        }
                        else {
                            if theWinningHandDetails[27] != "0" {
                                Textual content("Nibs: 2")
                                    .foregroundColor(.white)
                                    .daring(true)
                                    .padding(.prime, -15.0)
                            }
                        }
                        
                    } //VStack
                } //HStack
                VStack {
                    HStack {
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                        }
                        else {
                            Picture(theWinningHandDetails[16])
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                        }
                        else {
                            Picture(theWinningHandDetails[17])
                                .padding(.main, -25.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                        }
                        else {
                            Picture(theWinningHandDetails[18])
                                .padding(.main, -25.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                        }
                        else {
                            Picture(theWinningHandDetails[19])
                                .padding(.main, -25.0)
                        }
                    } //HStack
                    if theWinningHandDetails.depend == 0 {
                        Textual content("Crib: ")
                            .foregroundColor(.white)
                            .daring(true)
                            .padding()
                    }
                    else {
                        Textual content("Crib: (theWinningHandDetails[25])")
                            .foregroundColor(.white)
                            .daring(true)
                            .padding()
                    }
                    
                    if theWinningHandDetails.depend == 0 {
                        Textual content("General Rating: ")
                            .foregroundColor(.white)
                            .daring(true)
                            .font(.title)
                    }
                    else {
                        Textual content("General Rating: (theWinningHandDetails[26])")
                            .foregroundColor(.white)
                            .daring(true)
                            .font(.title)
                    }
                    if theWinningHandDetails.depend == 0 {
                        Textual content("")
                            .foregroundColor(.white)
                            .font(.title)
                            .daring(true)
                            .padding()
                    }
                    else {
                        Button("Share", motion: {
                                        let picture = self.snapshot()
                                        let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
                                        let _: Void? = UIApplication.shared.connectedScenes.map({ $0 as? UIWindowScene }).compactMap({ $0 }).first?.home windows.first?.rootViewController?.current(activityVC, animated: true, completion: nil)
                                    })
                        .padding()
                        .font(.title)
                    }
                } //VStack
            } //VStack

        } //ZStack
    }
}

extension WinningHandView {
    func snapshot(origin: CGPoint = .zero, dimension: CGSize = .zero) -> UIImage {
        let controller = UIHostingController(rootView: self)
        let view = controller.view

        let targetSize = dimension == .zero ? controller.view.intrinsicContentSize : dimension
        view?.backgroundColor = .clear
        view?.bounds = CGRect(origin: origin, dimension: targetSize)

        let renderer = UIGraphicsImageRenderer(dimension: targetSize)

        return renderer.picture { _ in
            view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
        }
    }
}

#Preview aofc

I’ve created dummy knowledge (which may be seen within the #Preview) and I put a short lived Nav Hyperlink in my ContentView to name this view with the dummy knowledge, and it renders completely. I am undecided why it is not doing so for the ListView. Any assist can be drastically appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *