Questions tagged [environmentobject]
120 questions
3
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…

Alina
- 61
- 5
2
votes
1 answer
SwiftUI How to have class viewable from all views
I am fairly new to SwiftUI and I am trying to build an app where you can favorite items in a list. It works in the ContentView but I would like to have the option to favorite and unfavorite an item in its DetailView.
I know that vm is not in the…

John Citrowske
- 75
- 4
2
votes
1 answer
SwiftUI view unexpectedly dismissing on change of state object values
Weird behavior where changing state is auto-dismissing a view.
Xcode 13.2.1,
MacOS 12.2.1,
iPhone 13 simulator iOS 15.something.
I'm genning up an object in the root view and passing it into the environment using @environmentObject(...), navigating…

ChrisH
- 975
- 12
- 21
2
votes
2 answers
SwiftUI: Binding on property of struct derived from environment object
I have two structs in my task manager app:
struct Task {
var identifier: String
var title: String
var tags: [String] // Array of tag identifiers
}
struct Tag {
var identifier: String
var title: String
}
I then have a class to…

Chris Schlitt
- 133
- 3
- 12
2
votes
0 answers
How to share an EnvironmentObject from a View to its ViewModel?
I'm using a DataStore environmentObject to pass data around my app, however I'm at a point where I needed to create a specific View Model class for a view, but I need to be able to pass the instance of my dataStore to my WorkoutDetailViewModel but…

GarySabo
- 5,806
- 5
- 49
- 124
2
votes
1 answer
SwiftUI sheet does not dismiss
Using Swift5.2.3, iOS14.4.2, XCode12.4,
Working with the .sheet modifier in SwiftUI made me feel excited at first since it seemed like an easy and efficient way to display a modal sheet.
However, inside a real-world application it turns out that…

iKK
- 6,394
- 10
- 58
- 131
2
votes
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…

Lex Brouwers
- 78
- 6
2
votes
1 answer
get EnvironmentObject property within method
I need to access an EnvironmentObject from a method within a View-Extension, so I cannot add an @EnvironmentObject property.
I tried to do it like
extension View {
func myFunc() {
let something = EnvironmentObject()
}
}
But…

swalkner
- 16,679
- 31
- 123
- 210
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 {
…

TJ D'Alessandro
- 109
- 1
- 19
2
votes
1 answer
SwiftUI pass by reference a class object to another view
I'm trying to learn SwiftUI and I'm going to develop a simple app with tab views and sharing core motion data between those views.
The main idea is to create a motion manager object (like here) and use the sensor values in all…

Murokill
- 87
- 1
- 7
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() {
…

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 {
…
user11960918
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…

mic
- 155
- 2
- 9
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
…

emelagumat
- 301
- 2
- 10
1
vote
1 answer
View not recognizing instance of @EnvironmentObject
I'm working on the audio player portion of my app and inside the player view, I have a simple if statement check. It looks to make sure there is an instance of the audioManager.player and if there is one, it created the vstacks/hstacks etc.
For some…

mzm0227
- 43
- 5