I'm creating a program where every time a user clicks a button, which is in the MyPond view, a new fish is added to an array, which appears as a FishView in a grid in the Journal View.
MyPond, where the user clicks a button to generate a new fish:
struct MyPond: View {
@State var newFish = Fish()
var body: some View {
VStack {
Button(action: {
newFish = Fish()
Journal().addFish(fish: newFish)
}
}) {
Text("Click here to fish")
.padding()
}
}
}
}
The Fish Object:
struct Fish: Identifiable {
var id = UUID()
var name: String = fishNameGenerated.randomElement()!
}
Journal, which is currently not displaying any fishViews:
class Fishies: ObservableObject {
@State var array:[Fish] = []
}
struct Journal: View {
@ObservedObject var fishArray = Fishies()
public func addFish(fish: Fish) {
self.fishArray.array.append(fish)
for fishy in fishArray.array {
print(fishy.name)
} // currently not printing anything
}
let columns = [GridItem(.flexible(), spacing: 10), GridItem(.flexible(), spacing: 10), GridItem(.flexible(), spacing: 10), GridItem(.flexible(), spacing: 10)]
var body: some View {
ScrollView() {
LazyVGrid(columns: columns, spacing: 10) {
ForEach(fishArray.array){ fish in
FishView(fish: fish) //Displays fish as fish view. FishView code is working
}
}
}
}
}