Questions tagged [environmentobject]

120 questions
1
vote
2 answers

Can't get SwiftUI View objects to update from @Published ObservableObject or @EnvironmentObject variables

I've done a ton of searching and read a bunch of articles but I cannot get SwiftUI to dynamically update the view based on changing variables in the model, at least the kind of thing I'm doing. Basically I want to update the view based on the app's…
1
vote
1 answer

Swift Preview problem with @EnvironmentObject

I am having trouble getting an updated preview when using @EnvironmentObject in my Swift project. In the simulator it works fine, but previewing is not. The error it shows is following: ASET - Audio System Engineer Tools crashed due to missing…
Fabian H.
  • 458
  • 4
  • 11
1
vote
1 answer

SwiftUI View Not Updating to Environment Object

I am having an issue getting the ContentBodyView to update properly when a button is pressed in the MenuView. When I print out the @Published currentPage var, it changes on button press, but doesn't appear to get carried across into ContentBodyView.…
cjochum
  • 79
  • 2
  • 7
1
vote
3 answers

SwiftUI @EnvironmentObject error: may be missing as an ancestor of this view -- accessing object in the init()

The following code produces the runtime error: @EnvironmentObject error: may be missing as an ancestor of this view. The tState in the environment is an @ObservedObject. struct TEditorView: View { @EnvironmentObject private var tState: TState …
LostInTheTrees
  • 1,135
  • 9
  • 19
1
vote
1 answer

Get @EnvironmentObject to NSObject class

I have a simple ObservableObject class which I initialize in the App.swift file and pass to the first view: final class Counter: ObservableObject {} @main struct MyCoolApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) private var…
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
1
vote
0 answers

@Published property in @EnvironmentObject doesn't update the root view of my application

I am trying to make a router object for my application using @EnvironmentObject. But the problem is the @Published property doesn't update the root view when the root view type is updated. How it should work A user clicks Sign in with Apple…
vcvcvcvc
  • 137
  • 4
  • 10
1
vote
2 answers

String variable in Environment is a get only. Swift

I can get a variable in my environment but I cannot set/update the variable. My environment object looks like this: import SwiftUI import Combine class UserData: ObservableObject{ @Published var examples = exampleData } I have a load function…
Louis Ye
  • 134
  • 1
  • 13
1
vote
1 answer

Swifui: MacOS App: Fetching Core Data in ContentView OK, but how to use the result from AppDelegate?

With Xcode 11.6 (11E708), SwiftUI, for a MacOS app, I have data generated in the ContentView.swift. But I cannot use those data inside the AppDelegate.swift: Build Succeeded but the print(contentView.order.item) (please see below) produce this…
kali mera
  • 13
  • 4
1
vote
1 answer

SwiftUI Subscribing to property change with EnvironmentObject

Here is my Model: class Channel: Identifiable, Decodable, ObservableObject { var id = UUID() var channelId = "" var title = "" var thumbnail = "" @Published var thumbnailImage : UIImage? enum CodingKeys: String,…
AvsBest
  • 435
  • 1
  • 4
  • 9
1
vote
1 answer

SwiftUI - ObservableObject, EnvironmentObject, SummaryView

After creating the code, i would like to create a summary view that allows me to view the values that have been chosen in the picker. How can I do? I read some forums about @ObservableObject and about @EnvironmentObject, but I can't understand…
user13952226
1
vote
1 answer

How do you make a two way binding within a class?

Using @EnvironmentObject I created a class that inherits from ObservableObject and is the hub for @Published global variables. I couldn't create a two way binding from within the class though. How can I accomplish that? The $ doesn't work in classes…
Rich
  • 57
  • 1
  • 6
1
vote
1 answer

How to change @EnvironmentObject from class, and not a View

I have a TabView with two child views - PageOneView and PageTwoView. I also have an @EnvironmentObject Data initialized in SceneDelegate. I also have a nested child for each of the PageOneView and PageTwoView. For PageTwoView the nested child is…
Alina
  • 61
  • 5
1
vote
1 answer

SwiftUI Fatal error: No ObservableObject of type found

I want a single point in my app where I can store some data and quick access it. Before SwiftUI I just had ApplicationDelegate for this purposes. Before I just needed to call kind of appDelegate.userService and it provides to me a single point in…
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
0
votes
1 answer

When using SwiftUI EnvironmentObjects, I get the error saying missing ObservableObject

I get the error Fatal error: No ObservableObject of type RegistrationViewModel found. A View.environmentObject(_:) for RegistrationViewModel may be missing as an ancestor of this view. struct ContentView: View { @StateObject var spotifyData =…
SeanMeek
  • 3
  • 2
0
votes
1 answer

Setting an @EnvironmentObject value when using AVAudioPlayerDelegate in a SwiftUI view hierarchy

I'm playing a sound in a SwiftUI view, and I'd like to set a value in an @EnvironmentObject when the sound completes. So, I declare protocol compliance, and my completion handler is duly called. BUT, I can't set a value in my @EnvironmentObject,…
Andromeda
  • 551
  • 1
  • 6
  • 15