In the app I am developing, I have multiple (23) screens that are the almost the same. They differ by a picture. The data for all of these can be kept in an array [String]. I tried calling a NavigationView (separate screen) inside a for loop but couldn't get it to work. Should this be possible? Is there any examples out there?
Asked
Active
Viewed 56 times
1 Answers
0
You can try and pass through 2 things between the views.
count
too keep a track of each view and aarray
to keep a track of each of the information inside of the view.
Here would be a somewhat example:
import SwiftUI
struct ContentView1: View {
@State private var count = 0
@State private var info = ["a", "b", "c"]
var body: some View {
VStack {
Text("View 1")
Text("Content: a")
NavigationLink(destination: ContentView2(count: count + 1, info: info)) {
Text("Next")
}
}
}
}
struct ContentView2: View {
var count: Int
var info: [String]
var body: some View {
VStack {
Text(count)
Text(info: info[count]
NavigationLink(destination: ContentView3(count: count + 1, info: info)) {
Text("Next")
}
}
}
}
struct ContentView3: View {
var count: Int
var info: [String]
var body: some View {
VStack {
Text(count)
Text(info: info[count]
}
}
}

anish1
- 27
- 6
-
Thank you for your response, but in this approach there is a list of navigational links all based on a central View. I've seen this before. I was wondering if there was a way to have a list of Views daisy-chained. Example: View1 will have "a" and a next navigational link to View2 which will have "b" and have a navigational link to View3 which will have "c", and so on. I'm not sure if that is possible – Huckleberry Carignan May 15 '23 at 12:11
-
I see. Here is something that if you definitely knew how many elements that would be in the array. Just pass through a count and the array between each view. I'm not a 100% sure how to do this. – anish1 May 16 '23 at 18:32