FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

Pass Variable To Sub View

Forums > macOS

I'm trying to figure out how to pass a variable to a sub view. To explain further..... My main view has the code...

HStack {
                    Rectangle()
                        .fill(self.headerData.rectangle1Color)
                        .frame(width: 100, height: 100, alignment: .center)

                    ColorGrid()

                }

As you can see, this is just a rectangle whose color is defined by the variable "rectangle1Color" We then call ColorGrid() which is defined below...

struct ColorGrid: View {

    @ObservedObject var headerData = HeaderData.shared

    var body: some View {
        HStack {

            Button(action: {
                changeColor(newColor: .blue)
            }) {
                Text("Blue").foregroundColor(.blue)
            }

            Button(action: {
                changeColor(newColor: .red)
            }) {
                Text("Red").foregroundColor(.red)
            }

            Button(action: {
                changeColor(newColor: .green)
            }) {
                Text("Green").foregroundColor(.green)
            }

        }// END OF HSTACK
    }// END OF BODY

    func changeColor(newColor: Color) {

        self.headerData.rectangle1Color = newColor

    }
}

This is a simple sub view with 3 buttons (red green blue) Each button passes a color in the changeColor() func which in turn updates the value of rectangle1Color.

My Question Is... If I want to use ColorGrid() in various different places, each controlling color of different variables - how can I specify in mainView which variable I want to adjust and then pass this to ColorGrid() and changeColor() accordingly??

I've tried various ideas but dont seem to be getting any closer

   

I did this and works

struct ContentView: View {
    @State private var fillColor = Color.clear

    var body: some View {
        VStack {
            Rectangle()
                .fill(fillColor)
                .frame(width: 100, height: 100, alignment: .center)

            ColorGrid(fillColor: $fillColor)
        }
    }
}

and for ColorGrid

struct ColorGrid: View {
    @Binding var fillColor: Color

    var body: some View {
        HStack {
            Button(action: {
                changeColor(newColor: .blue)
            }) {
                Text("Blue").foregroundColor(.blue)
            }

            Button(action: {
                changeColor(newColor: .red)
            }) {
                Text("Red").foregroundColor(.red)
            }

            Button(action: {
                changeColor(newColor: .green)
            }) {
                Text("Green").foregroundColor(.green)
            }
        }
    }

    func changeColor(newColor: Color) {
        fillColor = newColor
    }
}

however you do not need the func as you can do this

Button(action: {
    fillColor = .blue
}) {
    Text("Blue").foregroundColor(.blue)
}

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Get started

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

Reply to this topic…

You need to create an account or log in to reply.

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.