BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

How does one initialize Views as stored properties in the initializer of a parent view?

Forums > SwiftUI

**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]

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.

//methods to call from the parent view

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
Var body -> some View {

VStack {

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*/

//other smaller views with the same kind of issues initializing

So, am I missing something here? I’m very, very lost as to what I can do to make this possible.


Save 50% in my Black Friday sale.

SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.