0

I have the following:

struct Event: Identifiable {
    let id: Int
    let name: String
    let image: String
}

let events = [
    Event(id: 0, name: "Host Tournament", image: "cup"),
    Event(id: 1, name: "Post Club Info", image: "shield"),
    Event(id: 3, name: "Share A Post", image: "write")
]

I want to be able for each Event to hold a separate view.

For example Event(id: 0, name: "Host Tournament", image: "cup", destinationView: PostView())

let events = [
    Event(id: 0, name: "Host Tournament", image: "cup",destinationView: PostView()),
    Event(id: 1, name: "Post Club Info", image: "shield",destinationView: ClubView()),
    Event(id: 3, name: "Share A Post", image: "write", destinationView: StoryView())
]

So i can pass destinationView into my NavigationLink when i loop through events. Im not sure what type the PostView() should be defined as in my struct?

This is what Im currently doing:

ForEach(events) { event in
  NavigationLink(destination: //PASS VIEW HERE FROM EVENT) {
     VStack {
         Image(event.image)
          .resizable()
          .aspectRatio(contentMode: .fill)
          .frame(width: 100, height: 100)
          .padding(55)
          Text(event.name)
          .font(.system(.headline))
          .padding(.bottom,20)
         }
         .padding()
         .border(Color.black, width: 4)
         .cornerRadius(10)
       }.buttonStyle(PlainButtonStyle())
   }

I want to be able to pass in a view depending on the Event its looping through.

2
  • What do you mean by separate view? Different types? Would you provide more detail, scratchy example? Commented Apr 30, 2020 at 14:22
  • Just updated my question @Asperi Commented Apr 30, 2020 at 14:34

1 Answer 1

1

That's pure design to couple model with view so tightly... but if you want, technically it is possible to do in the following way

let events = [
    Event(id: 0, name: "Host Tournament", image: "cup",
         destinationView: AnyView(PostView())),
    Event(id: 1, name: "Post Club Info", image: "shield",
         destinationView: AnyView(ClubView())),
    Event(id: 3, name: "Share A Post", image: "write", 
         destinationView: AnyView(StoryView()))
]
Sign up to request clarification or add additional context in comments.

1 Comment

i think you might have misunderstood me, i have put more details in question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.