How to update @State variable in my View?

2 weeks ago 21
ARTICLE AD BOX
struct ContentView: View { @State var inputs1:Array<String> = ["*", "*", "*"] @State var inputs2:Array<String> = ["*", "*", "*"] var body: some View { VStack(alignment: .center) { let inputRow1 = createRow(inputs1[0], inputs1[1], inputs1[2]) inputRow1 let inputRow2 = createRow(inputs2[0], inputs2[1], inputs2[2]) inputRow2 let enterButton = EnterButtonView() enterButton } }

The createRow function is as such:

func createRow(_ letter1:String, _ letter2:String, _ letter3:String) -> some View { var aSimpleRow : some View { HStack(alignment: .center) { Text(letter1) Text(letter2) Text(letter3) } }

I need to use my Enter button to somehow update the @State var inputArray. For example the user taps and the array changes to ["%", "*", "&"] - and thus the corresponding text boxes also update to the same String values.

I created a button Struct:

struct EnterButtonView: View { var body: some View { Button(action: { enterButtonFunction() }) { // The label view HStack { Text("ENTER") } } } }

I can easily create a func that returns an updated array - but I don't see how I can integrate that return value into the Content View and thus update one of the inputArrays.

I have been reading up on @State variables and how they work, I think I'm supposed to use them here for this purpose but coming across this issue I could be way off. I've seen people reference binding but that seems to be to send information between 2 views. Maybe that's applicable here since the button is a view and the Content View is its own view. I'm just unsure on how to approach this.

Read Entire Article