|
I have a very simple 3/4 View thing. For context, I am complete Swift/SwiftUI newb... In my ContentView, I have an Observable written like this (I followed the examples here):
But that produces the error "Accessing State's value outside of being installed on a View. This will result in a constant Binding of the initial value and will not update." when I try and interact with it in my other View which looks like this:
When I press either button, that fires Is it a case of somehow needing to initialise that |
|
But where do you instantiate your class? You have to make an instance of that first, then you can pass it around using binding, or you can inject it in environment and access it from there. But you have to have "the state of truth" of the object somewhere like so:
|
|
@ygeras I have it instantiated in the view below, so my ContentView.swift starts like this:
I thought the second line in ContentView was doing that? |
|
What about this? |
|
OK, here is the complete contents I have. I have been reading and watching more things on this and I clearly don't understand this conceptually. I am only targeting iOS17+ so trying to use the new @Observable, @State and @Environment. From what I have read and watched, I believe that if I set a @Observable in my root ContentView, I can then access and amend the properties of that view, across different files, without needing to pass it in to each view. Is that correct? In the example code below, I want a property I can toggle, throughout my various views, that will bring in a menu view. So in my
That view then loads
So, am I misunderstanding how I use these @Observable work? Ultimately, I just want to be able to get and set that Bool from multiple files and have the views update on the basis of whether it is true/false. |
|
But wait you have this in your Home view in button action...
You're basically creating a new object which may change every single time the view is refreshed. And it can happen many many times. Your task is to create this object let's say in Content view and pass it to subviews either using @Bindable <- as this is used for observable objects, @Binding is used for simple types as Int, String, Bool etc. or via Environment, which allows you to access this object from any subviews without passing in each subview your object. In your Home view try instead of instantiating a new object, pass in that object from the place you have created it. |
|
OK, I have this working now, only problem is that all Previews apart from ContentView crash. I don't see any errors but I am unsure if I need to be passing something into the Preview? Here is what I did to get this working:
I have used the
It seems to work as expected. Does this seem 'correct'? Or is there something I am doing there that would obviously be messing up the previews? |
|
As far as I can recall you just need to add environment modifier to your view in Preview and instantiate your object there directly. So this should do the job.
|
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!
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.