NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

How do I make an if statement always look for change in a variable?

Forums > SwiftUI

I was playing around with having multiple views and wanted to make a picker in a different view change the look of another view. if I change the variable from 1 to 2 it does change the gradient. However using the picker does not change the gradient.


+1 for asking a great question in the Forums.

-1 for not copy/ pasting code into your message. If we want to test your code, you expect us to retype the whole thing?

To answer your question, you need to move beyond @State and learn about Bindings.

Here's a great video from @twostraws: Building Custom Component with Bindings

Code Example:

// Please use the code block to add code examples.
let plea = "Please use the code block to list code examples."


Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

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.