**I’m completely-blind, so please pardon the lack of indentation, as it poses issues with Voiceover.
I’m working on my first large SwiftUI project since finishing the 100 Days of SwiftUI, and running into problems when initializing nested views that rely on the properties being passed into a parent View’s initializer.
In my project I have a large View struct that is the main destination a user will end-up after the Home Screen of the app. To keep the code for this View manageable, I’ve created several smaller View types in other files that represent different portions of the parent view’s body. These smaller views read data from the parent view, and are capable of updating the values of the parent view via bindings.
With that in mind, I need those smaller views as stored properties in the parent view so that when the parent view needs to call methods contained within the smaller view structs.
As some of the data shared between views are custom types, I’m receiving the error that “self cannot be accessed” When initializing those custom values in the parent view’s initializer, and trying to pass them to the stored view properties in the same initializer.
First, here’s a custom type:
/* This type is filled with JSON data*/
struct NumberPuzzle: Codable {
Let title: String
Let allNumbers: [Int]
}//struct
Now, here’s an example smaller View:
Struct PuzzleView: View {
/* Instances of this button type move themselves from the shownButtons array to the selectedButtons array when pressed*/
//This is passed in from the parent View. It’s where the buttons are displayed
@Binding var DisplayedValueInParentView: String
@State var puzzle: NumberPuzzle
@State Var displayedButtons = [NumberButton]()
@State Var selectedButtons = [NumberButton]()
Var body -> some View {
//Code that initializes and shows all of the buttons and changes them visually to indicate which ones are selected.
}//body
//methods to call from the parent view
}//struct
And finally, here’s the parent view:
Struct ParentView: View {
@State displayedValue = “”
@State puzzle: Puzzle
@State puzzleView: PuzzleView
Init(puzzle: NumberPuzzle) {
self.puzzle = puzzle
//This is where the error comes from.
self.puzzleView = PuzzleView(displayedValueInParentView: displayedValue, puzzle: puzzle)
/* I’ve also tried the following:
self.puzzleView = State<PuzzleView>.init(initialValue: PuzzleView(displayedValueInParentView: displayedValue, puzzle: puzzle))
However, this still results in the same error
*/
}//init
Var body -> some View {
VStack {
Text(displayedValue)
Button(“Reset”) {
DisplayedValue = “"
/* This is where I need to access the arrays of buttons in the puzzleView property. I just want to return all of the values in the selectedButtons array back to the other array, and reset their appearances back to normal by calling methods contained inside the PuzzleView instance*/
}//button
//other smaller views with the same kind of issues initializing
}//Vstack
}//body
}//struct
So, am I missing something here? I’m very, very lost as to what I can do to make this possible.