I'm trying to navigate from one page to another page while parsing data between screens, I've created a model of the data that I would need to parse between the screens and set the data on the button press, is there something that I am missing here?
Model:
import Foundation
struct GroupType: Hashable{
var type: String?
var image: String?
}
Screen being navigated from:
struct SelectGroupTypeView: View {
@State var selectedGroupType: GroupType = GroupType()
private var groups: [GroupType] = [
GroupType(type: "Festivals", image: "Festivals"),
GroupType(type: "Parties", image: "Parties"),
GroupType(type: "Stores/Malls", image: "Stores"),
GroupType(type: "Restaurants", image: "Restaurants"),
GroupType(type: "Gyms and Excersize", image: "Excersize"),
GroupType(type: "Education/Study Groups", image: "StudyGroup")
]
var body: some View {
VStack{
ForEach(0..<groups.count/2) { row in
HStack {
ForEach(0..<2) { column in
let index = row * 2 + column
if index < groups.count {
Button {
self.selectedGroupType = GroupType(type: groups[index].type, image: groups[index].image)
print(groups[index])
} label: {
NavigationLink(destination: GroupCreationView(groupType: $selectedGroupType)) {
VStack{
Image(groups[index].image ?? "")
.resizable()
.scaledToFit()
Text(groups[index].type ?? "")
}
.frame(maxWidth: .infinity)
}
}
}
}
}
}
}
}
}
The file that should be receiving data keeps logging that both of the fields are nil or cant find an image '', but when clicking on the other page without navigating, the data gets logged correctly?
import SwiftUI
struct GroupCreationView: View {
@Binding var groupType: GroupType //This should be receiving data
@State var groupTypeString: String = ""
var body: some View {
VStack{
Image(groupType.image ?? "")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxWidth: 300)
}
}