0

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)

     }
}

DevAcc
  • 93
  • 9
  • `NavigationLink` is already a button, no need to embed it into another Button, remove the Button. If you want to perform some action as you press the `NavigationLink`, see: https://stackoverflow.com/questions/57666620/is-it-possible-for-a-navigationlink-to-perform-an-action-in-addition-to-navigati or restructure your code. – workingdog support Ukraine Aug 17 '23 at 22:08
  • Watch Demystify SwiftUI. You have a couple of unsafe practices in your code. Once you resolve those your links will work properly. – lorem ipsum Aug 18 '23 at 11:06

0 Answers0