HomeiOS Developmentios - SwiftUI: Why is my view pushed and popped instantly when...

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


I’ve just lately began working with iOS and SwiftUI. I ported a easy solitaire-type recreation over from Android and I’ve it largely working, however now I am attempting to implement a view that renders earlier successful arms. I am utilizing CoreData to retailer the successful hand values, winner’s title, rating and date. I’ve confirmed that the information is saved and fetched appropriately from CoreData. I can construct a listview that exhibits all of the successful arms, and when any merchandise within the checklist is clicked, the successful knowledge string ought to be handed to the WinningHandView to render the view, with an choice to share if the consumer so needs. The whole lot works nice till I click on on one of many successful scores, after which the app seems prefer it’s attempting to load the brand new view, however it instantly returns the app all the way in which to the ContentView (residence) display screen.

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

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.title ?? "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 assorted rows/columns of playing cards. I’ve verified that this worth is being handed and parsed appropriately:

import SwiftUI

struct WinningHandView: View {
    
    let winningHandDetails: String
    
    var physique: some View {
        ZStack {
            Shade.cardTableGreen.edgesIgnoringSafeArea(.all)
            let theWinningHandDetails = winningHandDetails.elements(separatedBy: "|")
            
            VStack {
                Textual content("Your successful 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(.high, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[1])
                                .padding(.high, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.high, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[2])
                                .padding(.high, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.high, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[3])
                                .padding(.high, -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(.high, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[5])
                                .padding(.high, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.high, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[6])
                                .padding(.high, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.high, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[7])
                                .padding(.high, -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(.high, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[9])
                                .padding(.high, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.high, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[10])
                                .padding(.high, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.high, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[11])
                                .padding(.high, -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(.high, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[13])
                                .padding(.high, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.high, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[14])
                                .padding(.high, -70.0)
                        }
                        if theWinningHandDetails.depend == 0 {
                            Picture("b1fv")
                                .padding(.high, -70.0)
                        }
                        else {
                            Picture(theWinningHandDetails[15])
                                .padding(.high, -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("Minimize card")
                                .foregroundColor(.white)
                                .daring(true)
                                .padding(.high, -15.0)
                                
                        }
                        else {
                            if theWinningHandDetails[27] != "0" {
                                Textual content("Nibs: 2")
                                    .foregroundColor(.white)
                                    .daring(true)
                                    .padding(.high, -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 1

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 isn’t doing so for the ListView. Any assist can be tremendously appreciated!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments