UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Environment and StateProperty Not working

Forums > SwiftUI

I have 1 file that is named TopMenu display using SwiftUI View and is not toggling the Text to change the color to it has been selected from the StateTestView.

And I have been trying to make it work and I simply can't figure it out.

One thing that I can't use is a Navigational Link. For the TopMenu Selection has to remain in view. As stated. This is a simplified version of my program with 3 Menu selection in a Vertical Stack. I could have it all in one File. But that would make my code to cumbersome and difficult to read. I prefer to use 1 view for each different row. And all 3 have to remain visible from the Main selection.

I have my sample Xcode program clearly layed out, to make it easy to figure out what is happening. the file in question is in the TopMenuView folder and the button to toggle environment variable is located in the StateTestView.

My Xcod project is here

Thank you ahead of time in trying to help me. robert

2      

Its not going to change because StateTestView and TopMenu each declare their own @StateObject var stateObject = TestObject(). So the stateObject being changed in StateTestView is different from and has no effect on the stateObject in TopMenu.

You also create yet another TestObject in your StateEnvironmentApp and inject it into your environment.

You only need one of those.

So, if you...

  1. Remove the line @EnvironmentObject var environmentObject: TestObject from ContentView (this isn't strictly necessary, but you don't actually use the object anywhere in this View so you don't need it)
  2. Add the line @EnvironmentObject var stateObject: TestObject to TopMenu and StateTestView
  3. Remove the line @StateObject var stateObject = TestObject() from TopMenu and StateTestView

... then they will all be using the same TestObject and it will work.

2      

First of all, Thank You For Looking At This.

I am doing something wrong. For the emphasis of the Error on stateObject FOUND THE PROBLEM: This is how I corrected the error. Is this safe? for the way I have corrected the error???

struct StateEnvironmentApp: App {

    @EnvironmentObject var stateObject: TestObject
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(TestObject())
        }
    }
}
struct TopMenu: View {

    @EnvironmentObject var stateObject: TestObject

    var body: some View {
        VStack {

            Text("Need to change Color and it is not working?")
                .multilineTextAlignment(.center)
                .padding()
            Text("A property wrapper type that subscribes ")
                .foregroundColor(stateObject.onTap ? .red : .blue)          // The error is on, "**stateObject**"

                // Getting an error: Thread 1: Fatal error: No ObservableObject of type TestObject found. A View.environmentObject(_:) for TestObject may be missing as an ancestor of this view.

3      

You didn't make any changes to your StateEnvironmentApp, did you? You still need to inject a TestObject into the environment there (or, really, any place above TopMenu and StateTestView in the view hierarchy) for this to work.

So, in StateEnvironmentApp you should have:

@StateObject var environmentObject = TestObject()

and

ContentView().environmentObject(environmentObject)

While in TopMenu and StateTestView you should have:

@EnvironmentObject var stateObject: TestObject

3      

YOU ARE A GREAT HELP. I am deeply appreciated by your kindness in helping me figure this out. It is now working great. I now have a better understanding on how it works. THANK YOU.

3      

I just wanna say thank you guys!

SwiftUI and Version 12.3

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.