|
Hi all This is a bit of a head scratcher. I'm running macOS Ventura beta 22A5295h, Xcode beta 3, but I have a feeling my issue with the code below is that I'm fundamentally misunderstanding something. As background, the wider issue I'm having is one where, after passing a bound string from an observed object to a child view, changes to that string in a SwiftUI TextField have some artifacts: the caret jumps to the end of the string, and keystrokes are lost. I've come up with a short example below, which doesn't show exactly the same behaviour, but still has the issue that the user cannot successfully edit the bound variable's value. Any ideas what I might be doing wrong? I guess I'm looking for the SwiftUI paradigm that allows editing of bound variable with a list.
Cheers Ian |
|
Ian has a question about bindings:
@twoStraws gives a pretty good example in his tutorials. See -> Pretty good binding example Not sure I could give a better example. What part of @twoStraw's example doesn't work for you? Try turning your fruit strings into identifiable |
|
Thanks, @Obelix
None. The paradigm I'm looking for is editing bound variables in a list where the data come from a view model, which isn't what the example is designed to demonstrate. Let's adapt the code to give it a basic view model, which is the first step towards my original example:
The good news is that this works. However, the editing should take place in a chlid view. Here's my adapted code. I get the correct behaviour (i.e. fruit name can be edited) when the EditView is not enclosed by a NavigationLink; howerver, when placed in NavigationLink, I'm back to the original, faulty behaviour (i.e. fruit name can't be edited).
At the moment, it looks as though the NavigationLink itself might be causing the issue. |
|
Some further testing shows that the example works when NavigationStackView is replaced with NavigationView. This must be a bug because NavigationStack is explicitly indicated as a replacement for NavigationView. I'll file a bug with Apple. Meanwhile, to be clear, this is solved. As a workaround for the above issue, use NavigationView instead of NavigationStackView for the time being. UPDATE: Looking further down Apple's documentation, there are statements suggesting NavigationStack should have a different approach when used like I am above, so this isn't a bug (I believe). However, this new approach doesn't seem to permit binding; when I create a binding and use it, I'm back to the previous failure mentioned above, where the text cannot be edited. So in the meantime, I'll use the NavigationView instead. UPDATE: I've got the behaviour I was looking for by having the view model store a temporary copy of the edited item, which is freely updatable without causing any views apart from the editor view itself to reload. I'll put it here in case it's helpful to anyone.
|
|
In the original code you were using the new These are not designed to work with each other. Both If you are using |
|
In the original code you were using the new These are not designed to work with each other. Both If you are using |
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 free gifts!
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.