I have 8 views for where in a horizontal scroll group, the user would tap the image and go to the corresponding view. I know I could do this manually but using a ForEach loop will save a lot of code, I've done similar things with text as you can see below and I tried to do so with the array of views, but the parameter requirements for a navigation link makes it difficult to refer to the view itself, as it would be ie. [dogs_and_cats but the navigation link wants it to be dogs_and_cats()]. Yet this doesn't work due to the errors: Type 'Any' has no member 'init' in the nav link line and Cannot convert value of type 'Barcelona_Museum_of_Contemporary_Art.Type' to expected element type 'Any.Protocol' for each of the array elements. If you were in my shoes how would you create a array of view objects if that is possible, and loop through them for each nav link?
let museumNamesForLink = [Whitney_Museum_of_American_Art,
The_Andy_Warhol_Museum,
Museum_of_Modern_Art, Nakamura_Keith_Haring_Collection,
Tate_Modern,
The_Broad_Museum,
Museum_fu_r_Moderne_Kunst,
Barcelona_Museum_of_Contemporary_Art]
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .top, spacing: 0) {
ForEach(museumNames.indices) { index in
VStack {
NavigationLink(destination: museumNamesForLink[index]()) {
Image(museumNames[index])
.resizable()
.aspectRatio(contentMode: .fit)
}
Text(museumNames[index])
.bold()
.font(.callout)
.foregroundColor(.white)
.multilineTextAlignment(.center)
.padding(.leading)
}
}
}
}