|
I have an
This is passed into the main app as an environment. Inside my ContentView I want to toggle a popover/sheet on the basis of whether The At the top of ContentView I have this:
And then further down, on a ZStack I have this:
This produces the error I have also tried this:
Which doesn't error, but also does not show the I have also tried using onChange like this:
And again, this does not error but the popover is not shown either. How can I link the display of the popover to the changing count of items in the |
|
Hi @Obelix I'm new to SwiftUI so certainly been useful to read through your code samples for better ways to structure things. However, my question is whether, in principle, it is possible to use to present a sheet or popover, purely on the basis of a Observed array changing. I'm unsure at this point if I'm fundamentally approaching the problem of how to present this popover/sheet in the wrong way? As I'm not seeing errors, and I can see the array contents changing, I'm unsure if there is some kind of binding I should be doing? |
|
Thanks for the additional code. I am unsure if I lack sufficient comprehension of your solution or I have just not explained myself very well. Probably both! I will attempt to re-iterate my requirements/problem with a simpler shopping list analogy. Sorry you are having to spoon feed me here! Suppose we have our However, in order to actually add an item to our As these So...
The crux of my issue is getting the So suppose the user is on the ChildView, and the Does this explanation alter your proposed solution? Or do I simply lack understanding at this point? |
|
At present, I am only able to get the desired outcome by doing this in the @Observable:
This is being passed into MainView as an environment modifier like so:
That recieved into MainView like this:
Then made bindable in that view's body with this:
And then the popover is presented like this:
However, the bit that feels a bit skanky is in order to update that
And then have the logic of updating the @Observable like this:
That feels 'off' to me. I feel that it should be possible to toggle the popover merely by asking whether the itemsIsEmpty, and have that be dynamic and react to be true/false and present/hide the popover accordingly. Having to directly set the Bool from a child view seems weird and I will have a lot of repeated code for every button that could add an item to the bastket?? |
|
How about adding to You can get rid of |
|
Thanks @bobstern, I did not know about the |
|
Do other peoples replies get automatically deleted when a solution is ticked? That's pretty annoying as there was plenty of interesting stuff in other peoples replies that would be useful for future travellers. |
|
No replies get deleted when an answer is marked solved. Occasionally I delete replies that are from spam bots, some of whom try to be clever by using ChatGPT to add some "helpful" replies to their account before they wade in with spam. |
|
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and more!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.