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

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 In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

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.