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 principally working, however now I am attempting to implement a view that renders earlier profitable palms. I am utilizing CoreData to retailer the profitable hand values, winner’s title, 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 palms, and when any merchandise within the listing is clicked, the profitable knowledge string ought to be handed to the WinningHandView to render the view, with an choice to share if the person so needs. Every part works wonderful till I click on on one of many profitable scores, after which the app seems to be 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.
Right here is the Corridor Of Fame Checklist View code that populates the listing from CoreData. The CoreData question completes efficiently and populates the listing:
import SwiftUI
import CoreData
struct HOFListView: View {
@FetchRequest(sortDescriptors: [SortDescriptor(.date)])
var winners: FetchedResults
var physique: some View {
ZStack{
Colour.cardTableGreen.edgesIgnoringSafeArea(.all)
VStack {
Textual content("Excessive Scores")
.font(.title)
.foregroundColor(.white)
.daring(true)
NavigationStack {
Checklist(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 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 {
Colour.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(.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("Reduce 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("Total Rating: ")
.foregroundColor(.white)
.daring(true)
.font(.title)
}
else {
Textual content("Total 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 5ofd
I’ve created dummy knowledge (which could 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 vastly appreciated!