|
I've got a simple app with two views. When one of them is tapped, I want to display in a sheet some information appropriate for that view. To distinguish between the views, I have an enum, and a couple of variables: to control which view was tapped, and whether to display the sheet. Here is the whole code:
When I run the app and tap the second view first, it behaves wrongly by displaying the sheet with ONE in it. I can tap the second view any number of times, and I'll keep getting the wrong result. But... Once I tap the first view, after that the app will behave as expected: tapping the second view will display TWO, tapping the first — ONE. But what is the reason for the initial hiccup? It appears that this line of code:
has no effect right after the app was launched. |
|
Hello, I am also relatively new to the whole swiftui declarative thing, but as I've been troubleshooting and debuggins similar issues, watching the code actually step through it finally clicked for me that the way swiftui is working is that it runs the code to render the view and then doesn't redraw the view unless something changed in the actual views, and it runs the stuff sequentially so the last thing it built was two, so if you keep tapping the two frame, nothing is changing. Not until you tap the first frame is something new in the view and it actually redraws the Content View one. I observed it when I was using a list with two different sets of data depending on row selected . Not sure fix for your scenario, but the way I fixed mine was with a Bool flag that I passed between the views togging on/off and adding a Text(flag).hidden in the view I wanted to refresh. Will play a little more with yours Hope this helps. |
|
Thank you, but in this particular example I am changing a @State variable when tapping on the second view. That should signal SwiftUI that the app's state has changed and so there is a need to re-create it. |
|
Hello, I am also relatively new to the whole swiftui declarative thing, but as I've been troubleshooting and debuggins similar issues, watching the code actually step through it finally clicked for me that the way swiftui is working is that it runs the code to render the view and then doesn't redraw the view unless something changed in the actual views, and it runs the stuff sequentially so the last thing it built was two, so if you keep tapping the two frame, nothing is changing. Not until you tap the first frame is something new in the view and it actually redraws the Content View one. I observed it when I was using a list with two different sets of data depending on row selected . Not sure fix for your scenario, but the way I fixed mine was with a Bool flag that I passed between the views togging on/off and adding a Text(flag).hidden in the view I wanted to refresh. Will play a little more with yours Hope this helps. |
|
I was using both @State and @Binding variables and the key was using Observable and Observed wrappers. Got it from a Paul Hudson tutorial explaing why @State does/does not work. https://www.hackingwithswift.com/quick-start/swiftui/whats-the-difference-between-observedobject-state-and-environmentobject |
|
hi Alexander, the problem is that the InfoView itself does not depend on the state. the solution is to make the InfoView depend on the state variable with a Binding. the suggested syntax would be
this is a little bit difficult to explain ... when BTW: if you remove the Binding in hope that helps, DMG |
|
There's a similar discussion over at stackoverflow. See -> Selecting a Target Sheet View Using guidance from that article I propose:
|
|
Thank you, @Obelix, but I am a bit puzzled by the solution you've suggested, to be honest. Also, using buttons isn't something I can do in the reall app, I have to resort to onTapGesture. |
|
Alexander follows up with:
Sorry! I used a different approach to invoking the sheet. You can open a sheet by providing it with an object, rather than toggling a binding.
You can see that I defined
However, as soon as you give the
Alexander clarifies:
How you set the I was just trying to get to the heart of the actions based on the binding value. So forgive me please, if I simplified your solution to focus on the troublesome code. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.