Questions tagged [environmentobject]
59 questions
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 {
…
![](../../users/profiles/1328838.webp)
Paul T.
- 4,226
- 6
- 39
- 84
6
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…
![](../../users/profiles/6742913.webp)
Dakata
- 914
- 2
- 8
- 22
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]
…
![](../../users/profiles/13288092.webp)
Ian
- 43
- 2
2
votes
2 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).…
![](../../users/profiles/160831.webp)
Pradyot
- 2,349
- 7
- 35
- 49
2
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…
![](../../users/profiles/4718413.webp)
alionthego
- 5,738
- 7
- 38
- 74
2
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…
![](../../users/profiles/14934149.webp)
Malburrito
- 194
- 1
- 10
2
votes
2 answers
Trying to access the environment object created inside scene delegate by another scenedelegate function
I have created an environment object in my scendelegate and want to also use it in another function that resides inside my scenedelegate. The code looks as follows:
SceneDelegate.swift
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
…
![](../../users/profiles/13383812.webp)
TJ D'Alessandro
- 31
- 1
- 12
2
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…
![](../../users/profiles/510199.webp)
Dennis Calla
- 669
- 7
- 8
2
votes
1 answer
SwiftUI NavigationLink @Binding of an array element causes Fatal error: Index out of range
I am having an observable object that is my unique source of truth :
class NetworkFetcher: ObservableObject {
@Published var list: [Car] = [Car(id: UUID().uuidString, name: "Tesla"), Car(id: UUID().uuidString, name: "BMW")]
func erase() {
…
![](../../users/profiles/13902453.webp)
Heliot Zakar
- 51
- 3
2
votes
1 answer
SwiftUI: Index out of range when deleting cells with toggle
I have problems with deleting cells that contain a Toggle.
My model looks like this:
class Model: ObservableObject {
@Published var items: [Item]
init(items: [Item]) {
self.items = items
}
}
struct Item: Identifiable {
…
![](../../users/profiles/11960918.webp)
Nico
- 213
- 1
- 7
2
votes
1 answer
Access @Environment object from UIViewControllerRepresentable object
I used this approach to incorporate camera with swiftUI:
https://medium.com/@gaspard.rosay/create-a-camera-app-with-swiftui-60876fcb9118
The UIViewControllerRepresentable is implemented by PageFourView class. PageFourView is one of the TabView of…
![](../../users/profiles/6150703.webp)
Alina
- 51
- 4
2
votes
1 answer
How to use edit an environment object from a class function?
I am new to swift programming. I was trying to play around with the environment object to update views by changing the value of the environment object specifically via a class function.
I have two subviews in the contentview as I wish to see if…
![](../../users/profiles/13534369.webp)
mic
- 135
- 1
- 6
2
votes
0 answers
SwiftUI. How to keep VIPER protocols adding EnvironmentObject?
I'm trying to use VIPER architecture with SwiftUI, using some protocols I was already using with UIKit.
These are my BasePresenter and Assembler implementations:
class BasePresenter : NSObject, Presenter {
typealias View = V
…
![](../../users/profiles/11010299.webp)
emelagumat
- 301
- 2
- 9
1
vote
0 answers
SwiftUI - Can't find EnvironmentObject in UITests
I'm trying to use @EnvironmentObject in my UITests, but I get an error at runtime.
Thread 1: Fatal error: No ObservableObject of type Person found. A View.environmentObject(_:) for Person may be missing as an ancestor of this view.
Doesn't…
![](../../users/profiles/2339600.webp)
Lex Brouwers
- 68
- 6
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…
![](../../users/profiles/15016386.webp)
Jordan
- 11
- 2