Questions tagged [environmentobject]
120 questions
25
votes
1 answer
What is the difference between @EnvironmentObject and @ObservedObject?
I have been reading about the property wrappers in SwiftUI and I see that they do a great job, but one thing which I really don't get is the difference between @EnvironmentObject and @ObservedObject.
From what I learned so far, I see that…

Dakata
- 1,227
- 2
- 14
- 33
10
votes
3 answers
Passing environment object between non-view classes in SwiftUI
I understand that EnvironmentObject property wrapper can be used to pass around objects to views. I have a session object which I am passing around to my views. Now I have a requirement to pass this into one of my model classes (i.e., non-view).…

Pradyot
- 2,897
- 7
- 41
- 58
10
votes
1 answer
How to access a global environment object in a class?
I have a class that needs to update a global environment object. I can pass that environment object between my structs all day, but how do I allow a class object to access the same variable?
import SwiftUI
class Global: ObservableObject
{
…

iphaaw
- 6,764
- 11
- 58
- 83
9
votes
1 answer
SwiftUI randomly crashes on presentationMode?.wrappedValue.dismiss()
This is how the crash looks like
So it randomly crashes on the UIKit line
UIKitCore
-[UIViewController _ancestorViewControllerOfClass:allowModalParent:] + 44
I have View in default SwiftUI navigation stack:
struct MyView: View {
…

Paul T.
- 4,938
- 7
- 45
- 93
8
votes
1 answer
SwiftUI prevent onReceive from firing on load
Is there any way to prevent onReceive from firing when the view initially loads when the variable I'm trying to receive is a @Published property of an @EnvironmentObject?
Here's the view:
struct ContentView: View {
@EnvironmentObject var…

Dennis Calla
- 839
- 9
- 10
5
votes
0 answers
How to check if @EnvironmentObject is set? (SwiftUI)
What I have: View and ViewModel (as extension to View struct).
Both of them are use @EnvironmentObject of type AppState.
The problem is that my preview crashes due to this error:
Fatal error: No ObservableObject of type AppState found.
Commenting…

Mikk
- 304
- 1
- 3
- 7
4
votes
5 answers
SwiftUI: @Environment(\.presentationMode)'s dismiss not working in iOS14
I have a view that shows a sheet for filtering the items in a list. The view has this var:
struct JobsTab: View {
@State private var jobFilter: JobFilter = JobFilter()
var filter: some View {
Button {
self.showFilter = true
…

noloman
- 11,411
- 20
- 82
- 129
4
votes
2 answers
Swiftui: @environmentObject - Cannot use instance member within property initializer; property initializers run before 'self' is available
I try to pass data from an @environmentObject to a @State object in the TopLevel
struct ContentView: View {
@EnvironmentObject var countRecognizer: themeCounter
@State var theme: themeModel = themeData[countRecognizer.themeCount]
…

Ian
- 43
- 3
3
votes
0 answers
SwiftUi 3.0 - @EnvironmentObject causing NavigationLink to pop
I've encountered a very odd bug in one of my apps on iOS 15 / SwiftUi 3.0 - can't find any info on it.
I have a series of 3 screens that each link to one another with a tag/selection NavigationLink as below:
NavigationLink(destination:…

swift--help
- 637
- 5
- 15
3
votes
1 answer
SwiftUI changing environment object re-created observed object in same view
So, I have few steps, last one contains EnvironmentObject and ObservedObject. The issue is, when I try to modify EnvironmentObject (lane 68) it re-creates ObservedObject.
Can any one explain me why this happens? Any solution to keep my…

George Heints
- 1,303
- 3
- 20
- 37
3
votes
0 answers
SwiftUI NavigationView Unexpected Pop to Root
I've created some SwiftUI code that use an EnvironmentObject to store boolean values for popping back to the root view and another EnvironmentObject to store a score variable that is intended to be used across multiple views.
In this example, I have…

Swifter
- 67
- 6
3
votes
2 answers
How to use EnvironmentObject in app file in SwiftUI
Is it possible to use EnvironmentObject in the app file for a new SwiftUI 2.0 project?
I'm using the following code and I get the error:
Fatal error: No ObservableObject of type Authentication found. A
View.environmentObject(_:) for Authentication…

alionthego
- 8,508
- 9
- 52
- 125
3
votes
1 answer
Casting UIWindow.rootView to UIHostingController which has environment variables
Casting UIWindow.rootView to a hosting controller like:
window?.rootViewController as? UIHostingController
works fine, unless it has environmentObjects.
As my MainTabView has been initialized with .environmentObject, I cannot cast it…

Rashid
- 1,515
- 16
- 16
3
votes
1 answer
SwiftUI: Using environment objects with Xcode 12/iOS 14 (--> How/where to place an object in the environment?)
in the process of programming my first iOS app I encountered a new issue that I was not able to find a solution for so far: I want to use an environment object to pass information to various views.
I know there are many explanations and tutorials…

Malburrito
- 860
- 7
- 23
3
votes
1 answer
SwiftUI How to pass a global variable to non-View classes
I understand how to use @EnvironmentObject to make global data available to any view class, but I can't find a way to pass the variable to a non-view class.
My situation, which seems like a common issue, is:
Login, returns an access token. This…

Steve Barden
- 616
- 1
- 6
- 16