I have a view that draws buttons, according to the state of an array of buttons. In that same strut, I have an add() method to add buttons to the array, which is giving me the following error:
iew.swift:60: warning run: Accessing State's value outside of being installed on a View. This will result in a constant Binding of the initial value and will not update.
Here’s the struct in question:
Struct rowOfButtons: View {
@State var displayedButtons = [Button]()
Var body -> some View {
Stack {
ForEach(buttons, id:.self) {newButton in
newButton
}//loop
}//Hstack
}//body
//methods
Mutating func add(newButton: Button) {
Buttons.append(newButton)
}//func
}//struct
With that, when instantiating new RowOfButtons objects, I create the instance, then loop over values of buttons that need to be added to the view, like this:
/Note: This is a simplified version of what I’m trying to do to make my post shorter. This would be inside some other View struct, and the returned array would be assigned to a property inside that struct /
Func initializeRowsWithButtons -> [RowsOfButtons] {
Let buttonNames = [/some array of button names returned from another function/]
//I also get an array that of integers that defines how many buttons will be in each row
Let arrayOfRowSizes = [/some returned array of integers/]
//This is the return array
Var filledRows = [RowOfButtons]()
//gets incremented in the loop
Var indexInButtonNames = 0
For I in arrayOfRowSizes {
Var newRow = RowOfButtons()
For _ in 0..<I {
/I’ve tried to ways to add to the array. Here’s the first: /
newRow.add(Button(buttonNames[indexInButtonNames])
/This is the other way:
newRow.displayedButtons.append(Button(buttonNames[indexInButtonNames]))
/
indexInButtonNames += 1
}//nested loop
filledRows.append(newRow)
}//loop
Return filledRows
}//func
Again, the add() method in the RowOfButtons astruct and the two attempts displayed in the above method throw the same error that I mentioned at the top of this post. I’m unsure of how else I can get around this. Does anyone have some suggestions?